Invalid permission error with fopen()

10 次查看(过去 30 天)
for i = 1:30
fid = fopen('%d.asc', i, 'r');
C = textscan(fid, '%s%s', 'Delimiter',',', 'HeaderLines', 1);
for k = 1:1046
E(k,1) = E(k,1) + str2double(string(C{1,1}(k,1)));
E(k,2) = E(k,2) + str2double(string(C{1,2}(k,1)));
end
fclose(fid);
end
E = (1/30) * E;
E
The code worked fine with reading just one file but it started giving permission errors when I put it in a loop.
The exact error message was :
Error using fopen
Invalid permission.
Error in physexp1 (line 2)
fid = fopen('%d.asc', i, 'r');
How would I fix this?
Thanks

回答(1 个)

Pulkit Goel
Pulkit Goel 2020-6-8
Saving file name in a variable worked for me. Try running the following code:
for i = 1:30
name=string(i)+'.asc';
fid = fopen(name, 'r');
C = textscan(fid, '%s%s', 'Delimiter',',', 'HeaderLines', 1);
for k = 1:1046
E(k,1) = E(k,1) + str2double(string(C{1,1}(k,1)));
E(k,2) = E(k,2) + str2double(string(C{1,2}(k,1)));
end
fclose(fid);
end
E = (1/30) * E;
E
I guess creating the name of file in fopen is causing the problem.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by