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.

+3


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

mexEvalString("drawnow;")

      

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

).

+9


source


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

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

test_mex_print.cpp



#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);
        ioFlush();
    }
}

      

Just compile it like: mex -largeArrayDims test_mex_print.cpp

+2


source


you can use mexPrintf

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

      

+1


source


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);");

+1


source


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

Try mexprintf ():

http://www.mathworks.co.uk/help/matlab/apiref/mexprintf.html

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, ...);

Arguments

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.

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

      

-1


source







All Articles