Invalid permission error with fopen()

9 views (last 30 days)
DONGUK KIM
DONGUK KIM on 28 May 2020
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

Answers (1)

Bhargavi Maganuru
Bhargavi Maganuru on 7 Jul 2020
Hi,
It might be an unappropriate way to format string using specifiers inside fopen. Instead you can try specifying filename seperately inside for loop and give that as input to fopen.
for i = 1:30
filename = sprintf('%d.asc', i);
fid = fopen(filename, 'r');
fclose(fid);
end

Community Treasure Hunt

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

Start Hunting!