1

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?

Amro
  • 123,847
  • 25
  • 243
  • 454
niels
  • 760
  • 8
  • 25

1 Answers1

2

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.

memyself
  • 11,907
  • 14
  • 61
  • 102
  • This doesn't work, but there has to be a way. Why does `msbuild` output show OK? – niels Sep 23 '11 at 15:47
  • 2
    then try to add additional statements that flush the output buffers in your 'myprog.exe' – memyself Sep 23 '11 at 15:59
  • You can also try: [status,result] = system('myprog.ext','-echo') or !myprog.exe – memyself Sep 23 '11 at 16:14
  • the first one already tried, the second doesn't work. Take a look at the question again, I edited it. It might ring a bell – niels Sep 23 '11 at 16:26
  • 1
    @memmyself: you were right it was a problem of correct flushing. Replacing some newlines with `std::endl` (which is also a flusher) it finally worked. I might have gotten something wrong when tested the first time. Modify your answer so I can accept it. – niels Sep 23 '11 at 16:37