error in using fscanf
6 views (last 30 days)
Show older comments
I have a script like this:
%Inputing POE
file_01='POE_0.01.txt';
fid=fopen(file_01, 'r');
FileName_01=fscanf(fid,'%g %g %g', [3,Inf])';
Mag_01=FileName_01(:,1); % moment magnitude
Dist_01=FileName_01(:,2); % distance
POE_01=FileName_01(:,3); % probability of exceedance
I = (mod(1:length(Mag_01),30)==1);
Mag = Mag_01(I);
Dist = Dist_01(1:30);
POE_01 = reshape(POE_01,30,15);
marginal(1:15,1) = sum(POE_01);
file_POE = ['POE_0.5.txt','POE_0.2.txt','POE_0.1.txt','POE_0.05.txt', ...
'POE_0.02.txt','POE_0.01.txt','POE_0.005.txt'];
for i = 1:7
file_temp=file_POE(i);
fid=fopen(file_temp, 'r');
FileName_temp=fscanf(fid,'%2.2f %g %g', [3,Inf])';
POE=FileName_temp(:,3); % probability of exceedance
marginal(1:15,i)=sum(reshape(POE,30,15));
temp=sum(marginal);
hold on
plot(Mag,marginal(1:15,i)/temp(i),'DisplayName',file_POE(i))
xlabel('Magnitude (Mw)');
ylabel('Contribution to the Hazard');
end
xlim([4.5 8.8])
legend
I got a massage about :
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in inputing_data (line 73)
FileName_temp=fscanf(fid,'%g %g %g', [3,Inf
Is there any one can help?
0 Comments
Answers (1)
Harshavardhan
on 31 Jan 2025 at 10:32
Hi @Skydriver
The script is probably failing because the file identifier “fid” is invalid. This happens when “fopen” is unable to find the file with the file name provided.
To solve this issue, ensure that file names are accurate and exist in the current folder or folder on the MATLAB path. If they aren’t in the current folder or folder on the MATLAB path, the full paths of the files must be provided.
For more information you can check out the documentation of “fopen” - https://www.mathworks.com/help/matlab/ref/fopen.html
Moreover, you can refer to this similar question - i had an error in my program with the fscanf i can't understand why it does not work?? thank you in advance - MATLAB Answers - MATLAB Central
Hope this helps.
0 Comments
See Also
Categories
Find more on Adding custom doc 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!