如何在运行时使mex函数printf? [英] how can I make a mex function printf while it's running?

查看:176
本文介绍了如何在运行时使mex函数printf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的MATLAB脚本中调用了一个mex文件. mex函数可能需要一段时间才能运行,因此,为了防止我的代码在没有任何输出的情况下停下来",我在mex文件中放置了许多printf语句,以输出有关正在处理的数据的一些运行信息.

但是当我调用mex函数时,它不会printf任何东西,并在int运行期间停留在该位置.最后,完成工作后,它将printf我想要的所有信息-不是在运行时而是在 完成后.这不是我想要的.

所以我想知道如何不仅使printf我想要的东西,而且在我想要的时候printf.

解决方案

是的,mexPrintf是您所需要的.但是请注意,命令窗口不会强行刷新其使用的缓冲区,通常会导致打印消息之前很长的延迟.如果您在调用mexPrintf之后开始大量计算,则会发生这种情况.

一种解决方法是使用

mexEvalString("drawnow;")

每次调用mexPrintf

.

如果您发现它不那么吸引人,则可以制作一个宏来调用这两个宏:

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

这使用可变参数宏__VA_ARGS__.它可能不是标准的一部分,但似乎是在GCC和Visual C ++中.就像调用printf(或mexPrintf)一样,只需调用printfFnc.

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.

解决方案

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

这篇关于如何在运行时使mex函数printf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆