Take the 2-minute tour ×

Say that, on a MATLAB interactive session, I call a function from a third party library output = long_execution(input). This function prints information via disp statements to the command window. I would like to capture the output of such disp statements on a text string that I can manipulate in MATLAB.

Is there a (hopefully easy) way of redirecting the output of disp to a text string? If so, how would you do it? (maybe via the overlading of disp?)

share|improve this question
possible duplicate of Suppressing a function's command window output in Matlab –  Amro Jul 11 '11 at 20:04
add comment

2 Answers

up vote 5 down vote accepted

You can use evalc function to capture disp outputs. For example,

    [T, output] = evalc('long_execution(input)');.

Anything that would normally go to command window is captured into the output T.

share|improve this answer
add comment

If everything is going into stdout, you can use the diary function to capture that and write it to file, then after execution you can use any number of matlab file reading utilities to parse through it. You might also find the function tempdir and tempname useful in this context.

share|improve this answer
add comment

Your Answer


