在Matlab中更改默认输出文件处理程序

时间:2013-01-18 18:30:05

标签: matlab stdout printf

我认为这会很有用,至少我发现了,我搜索过但没找到任何合适的。

假设您有一组使用stdout的行,或者这些行包含在函数中。然后我需要将输出流更改为文件。但不是日记()等。

示例:

ShowResults(...)   % this is a function containing a lot of fprintf('asdasdasd', ...)
                   % which by default shows messages on monitor 

然后我需要类似的东西:

ShowResults(...)   % this will now output to monitor
setOutputHandler(my_file_pointer); % setup redirection
ShowResults(...)   % this will now output to the file
setOutputHandler(stdout);

甚至更好的东西:

setOutputHandler(stdout, my_file_pointer);
ShowResults(...)   % this will now output to the file and monitor at the same time
setOutputHandler(stdout);

1 个答案:

答案 0 :(得分:1)

特别是如果在函数中使用fprintf,最简单的方法是定义一个额外的输入,在每次调用fprintf

时用作第一个参数

默认情况下,该附加输入将设置为0,这意味着fprintf将打印到屏幕。或者,您可以将fopen创建的文件标识符传递给showResults,以便fprintf写入文件。

如果这是不可行的,您可以随时使用capturedOutput = evalc('showResults(...)'),它将捕获数组中的所有输出,您将从中写入文件。

相关问题