How to make sure completion of a write to file

5 visualizzazioni (ultimi 30 giorni)
Art
Art il 9 Ago 2012
Within a function B (which is called from another function A) I am using multiple fprintf commands to print a matrix to a file. The file is opened and closed inside the function B. In faster machines it happens that the matrix does not finish printing, however the function B terminates and gives back control to the calling function A.
I have noticed that despite the fclose commmand at the end of function B, the file is still open when controls returns to calling function A. And if A does not close the file I can stil write to it or close it from the Matlab console. If I do that (closing from console) then all missing data is flushed to the file, but if I don't (for example if I quit Matlab) then a piece of the matrix will never be printed. Opening as 'w' or 'W' does not make any difference.
I have found a workaround: opening in A and passing the file id as parameter to function B, let function B do the printing and closing the file in A. This way insures that the whole matrix is printed, but it is very inconvenient because the function B should print in a large number of files (names changing all the time) and this workaround limits my ability to use it as expected.
I would appreciate some help.

Risposte (2)

Walter Roberson
Walter Roberson il 9 Ago 2012
In theory, if you perform any fseek() operation on the output file, it should flush. At least that is what the standards say for the underlying POSIX fseek() operation; I am not certain that it works that way for MATLAB.
Could I suggest that you experiment with using an onCleanup() to close the file? That would catch any cases where the function somehow returned early before reaching the fclose() in your logic.
  2 Commenti
Art
Art il 9 Ago 2012
Thaks for the suggestions, but I do not see how could I use any of them: fseek is just for read modus, but I am only writing. cleanup is an specific function of Code Composer Studio, how could I use it here? Looking at cleanup I have found other two functions that could be useful: execute and run, but again, I don't see how to use them in a simple function call.
Walter Roberson
Walter Roberson il 9 Ago 2012
fseek() is not just for read modes: you can fseek() on any stream. fseek() to 0 bytes relative to current position if you need to.
onCleanup() has nothing to do with code composer. The "on" is part of the name.

Accedi per commentare.


Art
Art il 9 Ago 2012
I have found another workaround. Doing a fclose('all') in calling function A just before it finishes, solves the issue: the offending file is flushed and closed. But the situation seems to be a Matlab bug. The fclose in function B is in fact never reached, but control is returned to function A.
Although this workaround works, there are really a number of files open (all the files open by function B) until function A closes them all.
I would appreciate a comment from some Matlab developer.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by