MATLAB Answers

0

Is there a way to flush the output buffer in MATLAB when writing to a file?

Asked by MathWorks Support Team on 27 Jun 2009
Latest activity Answered by Ba Mo
on 14 Sep 2019
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.

3 Answers

Answer by MathWorks Support Team on 18 Oct 2013
 Accepted Answer

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.

  3 Comments

Is this still the case? We just switched from running our automated tests with R2012a to R2012b: They now frequently fail with files missing and/or random time-outs.
This might suggests that there's some kind of file-system synchronization problem.
Is it possible that the way MATLAB handles I/O has changed between these two release versions? Perhaps we now need to explicitly flush files after writing them and before forking a job to read them?
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
This answer is only partial. It does not answer the second half of the question, which I now have as well, being:
Is there a way to flush the output buffer in MATLAB when writing to stdout?

Sign in to comment.


Answer by almog shalom on 6 Nov 2015

The command drawnow('update') will do the job.

  2 Comments

drawnow update is not documented for this purpose, and the documentation gives the impression that the flush could be skipped under some circumstances.
You are right. It is not documented. I checked it again and it does not always work.

Sign in to comment.


Answer by Ba Mo
on 14 Sep 2019

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 Comments

Sign in to comment.