Why are mex file output messages using mexPrintf() + mexEvalString("drawnow") not printed immediately in MATLAB R2015b?

조회 수: 9 (최근 30일)
When I run mex files which include some status messages using combinations of mexPrintf("The message") and mexEvalString("drawnow"), the output (in the Matlab Command Window) is only shown after complete execution of the mex function. Running the same mex file in R2014b, the message is shown immediately.
An example code might look like this:
#include <mex.h>
#include <stdlib.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Test\n");
mexEvalString("drawnow");
_sleep(5000);
return;
}
  댓글 수: 4
Eydrian
Eydrian 2016년 1월 15일
I experienced the same problem with 2015b and would appreciate a solution. If you for example have a progress indicator running:
mexPrintf("Converting:%3i%%",0);
while(progress < nrJobs) {
convertThing();
mexPrintf("\b\b\b\b%3i%%", progressInPercentage);
mexCallMATLAB(0, NULL, 0, NULL, "drawnow");
progressInPercentage = 100 * ++progress / nrJobs;
}
Since 2015b it only shows the last line: Converting: 100%
MATLAB equivalent:
nrJobs = 1000;
progress = 0;
fprintf('Progress: %3.0f%%\n', 0);
while progress < nrJobs
progressInPercentage = 100*progress/nrJobs;
fprintf('\b\b\b\b\b%3.0f%%\n', progressInPercentage);
drawnow();
progress = progress + 1;
end
This is working but is in MHO a hack:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int nrJobs = 1000;
int progress = 0;
int progressInPercentage = 0;
mxArray *timeout = mxCreateDoubleScalar(0.00001);
mexPrintf("Converting:%3i%%\n",0);
mexCallMATLAB(0, NULL, 0, NULL, "drawnow");
while (progress < nrJobs) {
progressInPercentage = ++progress * 100 / nrJobs;
mexPrintf("\b\b\b\b\b%3i%%\n", progressInPercentage);
mexCallMATLAB(0, NULL, 0, NULL, "drawnow");
mexCallMATLAB(0, NULL, 1, &timeout, "pause");
}
}
Tim Davis
Tim Davis 2020년 7월 30일
This "drawnow" hack works fine when running MATLAB on a local machine, but it is exceedingly slow if I'm logged in remotely (using "matlab -nodesktop"). I have a large library I'm developing, in C, with a mexFunction interface. The library itself is compiled outside of MATLAB. It can take a pointer to a printf function, so I can give it a pointer to mexPrintf to use instead of the standard ANSI C11 printf. I can also pass it a function pointer to a function with the same signature as the ANSI C11 "flush".
So I wrote a "flush" function that just does:
int flush_helper (void) { return (mexEvalString ("drawnow ; pause (1e-8) ; ")) ; }
So far so good ... it works fine when working on my local machine. It takes about a tenth of a second per flush when running remotely, so printfs (using the pointer to mexPrintf then calling the flush_helper) are exceedingly slow. I'm using R2018a.
The workaround posted below is awkward since I need a fully-ANSI-C-compliant printf with va_arg inputs.
What I really need is a MathWorks-supported mexFlush command to flush any output from mexPrintf.
Is there a better workaround than "drawnow"?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2016년 3월 30일
편집: Jan 2016년 3월 30일
If the overhead of calling Matlab cannot be avoided, the output to the command window can be created in Matlab directly:
function OutputHelper(Fmt, Arg)
fprintf(Fmt, Arg);
drawnow;
And the calling from the Mex file:
#include <mex.h>
#include <stdlib.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *Arg[2];
Arg[0] = mxCreateString("%s\n");
Arg[1] = mxCreateString("Test");
mexCallMATLAB(0, NULL, 2, Arg, "OutputHelper");
_sleep(2000);
return;
}
Now "Test" appears immediately even in Matlab 2015b before the 2 seconds delay.
I had no luck with trying to flush the event buffer of the command window text using Java methods.
  댓글 수: 1
Joris Gillis
Joris Gillis 2016년 3월 30일
I wonder what the crucial ingredient is here. Would it still work with mexprintf and OutputHelper-wrapped drawnow, or conversly with OutputHelper-wrapped fprintf and a mex-call to drawnow?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by