Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

c++ - how can I make a mex function printf while it's running?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...