Why does FCLOSE not close multiple open files when passed a vector of file identifiers in MATLAB 7.1 (R14SP3)?

6 次查看(过去 30 天)
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

采纳的回答

MathWorks Support Team
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 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R14SP1

Community Treasure Hunt

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

Start Hunting!

Translated by