0

我认为这会很有用,至少我觉得有用,我搜索但没有找到任何合适的东西。

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

例子:

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);
4

1 回答 1

1

特别是如果你使用fprintf在函数中使用,最简单的方法是定义一个额外的输入,您将在每次调用时将其用作第一个参数fprintf

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

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

于 2013-01-18T19:18:07.947 回答