开发者

Redirecting MATLAB's disp to a text string

开发者 https://www.devze.com 2023-03-19 06:11 出处:网络
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 comma

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消