Why does FCLOSE not close multiple open files when passed a vector of file identifiers in MATLAB 7.1 (R14SP3)?
7 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Edited: MathWorks Support Team
on 24 Sep 2014
When I pass a vector of file identifiers to the FCLOSE function in MATLAB 7.1 (R14SP3), it only closes the first file and leaves the rest open. It should either close all files or warn the user that some files were left open.
The following steps should reproduce the problem:
f1 = fopen('a.txt', 'w');
f2 = fopen('b.txt', 'w');
f3 = fopen('c.txt', 'w');
f = [f1 f2 f3];
fclose(f)
fstillopen=fopen('all')
The variable "fstillopen" contains the following result:
fstillopen =
4 5
Accepted Answer
MathWorks Support Team
on 18 Oct 2013
The ability to close multiple files with FCLOSE by providing a vector of file identifiers is not available in MATLAB 7.1 (R14SP3).
Note that MATLAB 7.2 (R2006a) produces an error when a vector of file identifiers is passed to FCLOSE.
As a workaround, you can close multiple files as defined with a vector of file identifiers by using a FOR loop and the FCLOSE command. For example:
f1 = fopen('a.txt', 'w');
f2 = fopen('b.txt', 'w');
f3 = fopen('c.txt', 'w');
f = [f1 f2 f3];
for i=1:length(f)
fclose(f(i));
end
fstillopen=fopen('all')
Or, if you would like to close all open files, you can enter the following:
fclose('all')
0 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!