how can I make a mex function printf while it'

2019-02-18 17:56发布

问题:

I have a mex file called in my MATLAB script. The mex function may take a while to run, so in order to prevent my code from "stopping there without any outputs", I put many printf statements in the mex file to output some running information about the data being processed.

But when I call the mex function, it doesn't printf anything and stays there during int's running. Finally, after finishing its work, it will printf all the information I want -- NOT while it is running but after finishing. It's not what I want.

So I want to know how to make it not only printf what I want but also printf at the time I want it.

回答1:

Yes, mexPrintf is what you need. But note that the command window does not forcibly flush the buffer it uses, often resulting in very long delays before your message is printed. This happens if you begin heavy computations after calling mexPrintf.

A workaround is to use

mexEvalString("drawnow;")

after each call to mexPrintf.

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

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

This uses the variadic macro __VA_ARGS__. It may not be a part of a standard, but seems to be in GCC and Visual C++. Just call printfFnc like you would call printf (or mexPrintf).



回答2:

There is an undocumented C++ function that resides in libmwservices.dll. It apparently flushes the output buffer. Here is 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();
    }
}

Simply compile it as: mex -largeArrayDims test_mex_print.cpp



回答3:

You can use mexPrintf

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


回答4:

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

However, using mexEvalString("pause(.001);"); after mexPrintf() did fix it. It took me some trial-and-error to figure out, so I hope this might be useful for future reference.

TLDR: use mexEvalString("pause(.001);");



回答5:

printf prints to stdout which isnt 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 a 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.