Search code examples
matlabcommand-window

force c++ program to show 'synchronized' output to MATLAB command window using dos()


I'm executing my c++ compiled program in MATLAB with dos('myprog.exe'). myprog produces some output that it is printed to the MATLAB command window only after myprog.exe finishes execution.

Is there a way to force MATLAB print the output when it is produced by myprog.exe and not at the end?


Solution

  • ANSWER Make sure that you are flushing correctly the output buffers in your c++ program. In my experience it sometimes helps to insert additional flushing commands (not just end of lines commands) to your code:

    std::cout << std::endl;
    

    NOTE You might also try to call your program like this:

    [status,result] = dos('myprog.exe','-echo') 
    [status,result] = system('myprog.exe','-echo') 
    

    The matlab help says: "'echo' forces the output to the Command Window, even though it is also being assigned into a variable."

    However this might not work because (again matlab help): "Console programs never execute in the background. Also, the MATLAB software always waits for the stdout pipe to close before continuing execution. " This means, that matlab might wait until your program finishes its execution before it shows you the console output. In that case there's nothing you can do about it.