How can I create a mex printf function while it is running?

I have a mex file that is being called in my MATLAB script. The mex function can take a while, so to keep my code from stopping there without any exits, I put several statements in the mex file printf

to output some current information about the data being processed.

But when I call the mex function it is printf

nothing and stays there while the int is running. Finally, after finishing work, it will have printf

all the information I want - NOT while it is running, but after finishing. This is not what I want.

So, I want to know how to do it not only printf

what I want, but also printf

at the time when I want it.


source to share

5 answers

Yes, mexPrintf

that's what you need. But note that the command window does not forcefully clear the buffer it is using, often leading to very long delays before your message is printed. This happens if you start heavy computation after the call mexPrintf


A workaround is to use



after every call mexPrintf


If you find yourself unappealing, you can make a macro that calls both:

#define printfFnc(...) { mexPrintf(__VA_ARGS__); mexEvalString("drawnow;");}


A variable macro is used here __VA_ARGS__

. It may not be part of the standard, but it seems to be in GCC and Visual C ++. Just call printfFnc

what you would call printf

(or mexPrintf




There is an undocumented C ++ feature that is in libmwservices.dll

. It appears to be clearing the output buffer. Here's an example:


#include "mex.h"

#pragma comment(lib, "libmwservices.lib")
extern bool ioFlush(void);

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    for(int i=0; i<100000; i++) {
        mexPrintf("%d\n", i);


Just compile it like: mex -largeArrayDims test_mex_print.cpp



you can use mexPrintf

* mex equivalent to MATLAB "disp" function
extern int mexPrintf(const char *fmt, /* printf style format */
                     ... /* any additional arguments */);




I had the same problem as OP where mexPrintf () did not print any output until the mex file finished running. Also, mexEvalString ("pullow;"); apparently didn't fix the problem, or at least it didn't with my setup (MATLAB2015b with MinGW 64bit C ++ 11 MEX code compiler)

However, using mexEvalString ("pause (.001);"); after mexPrintf () fixed it. It took me a bit of trial and error, so I hope this might be helpful for future references.

TL; DR: use mexEvalString ("pause (.001);");



printf prints to stdout which is not the Matlab screen. (It is hidden by default and collected / displayed at the end)

Try mexprintf ():

In a C MEX file, you must call mexPrintf instead of printf to display the string.

C Syntax

#include "mex.h"
int mexPrintf(const char *message, ...);


String to display. In C, the string can include conversion specifications, used by the ANSI® C printf function.

In C, any arguments used in the message. Each argument must have a corresponding conversion specification.

Number of characters printed including characters specified with backslash codes, such as \n and \b.




All Articles