Is there a way to flush the output buffer in MATLAB when writing to a file?
65 次查看(过去 30 天)
显示 更早的评论
I would like to flush the output buffer when writing to a file. I would also like to flush it when writing to STDOUT or STDERR.
采纳的回答
MathWorks Support Team
2013-10-18
The default behavior of MATLAB File I/O commands FPRINTF and FWRITE is to flush the buffer after every call when writing to a file other than the special output streams STDOUT and STDERR. There is, therefore, no need to explicitly flush the output buffer in this case.
1 个评论
Abhishek Pandey
2016-4-18
Hi Mark,
This behavior should not have changed between R2012a and R2012b. If you are still running into issues with this, please contact MathWorks Technical Support and raise a help ticket. I am sure our team will be very happy to help you.
- Abhishek
更多回答(1 个)
Ba Mo
2019-9-14
I would like to, respectfully, disagree with MATLABs' developers.
I have used MATLAB on a super-computer cluster, where i had dozens of jobs running and couldn't see what's happening till the end. Of course it depends on the type of HPC scheduler you're using. with LSF you can just use bpeek <job_name>. you dont need to flush the output
but with PBSprofessional and SGD, you just can't take a peek at the job, and the diary won't be written till the job is over.
For that, i came up with super duper solution; however, it is dependent on what is the function that you're running, which takes ages to finish.
In my case, i was running particleswarm and ga. these two functions have an option "outputFcn", which i was able to exploit.
1) everytime you call diary(<diary filename>.txt) the diary will be flushed.
2) the problem is that outputFcn and plotFcn expect an output of 0 or 1 (continue running or stop, respectively)
3) i like single-liners, i dont want to write a separate function for this. i want to do this with an anonymous function
the solution is to use the "evalc" function. inside the "evalc", you carry out these commands:
a) run the diary(<diary filename>.txt) command to flush the output; this won't stop recording the diary. it just tazes matlab into flushing the diary
b) ask matlab to output a good old '0'. a problem here is that "display(0)" and "disp(0)" will output a line-break and then our beloved zero, and another line-break. the way to do it is use "fprintf"
c) take the output from "evalc", which is a string '0' and convert it back to a numerical value
all in all, the magic trick is this:
flush_diary = @(optimValues,state) str2num((evalc(['diary([''',pathname,'diary.txt'']);fprintf(num2str(0))'])));
pso_options = optimoptions('particleswarm',..., %your other options here
'OutputFcn',flush_diary);
where "pathname" is where you want to save the diary
it has to be a single liner; i know it looks ugly but you can't break it down to several nested anonymous functions
most big matlab processes have something similar to an "outputfcn" feature. try to make use of them and read their documentation.
=======================
another solution:
if you have a sub-routine or function that you call repeatedly (i.e. the function where you evaluate the cost of a solution), in that function just write: diary([pathname,'diary.txt']);
if you are using parallel-computing, some unexpected behavior may occur; but i dont think it'll cause an error/failure
cheers
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!