Error using fscanf Invalid file identifier.

조회 수: 11 (최근 30일)
幸一
幸一 2024년 1월 10일
댓글: 幸一 2024년 1월 17일
error at the line 63 of the attached code.
I want to read the data files and make a coupling file. sample data file and the code has been attached to the thread.
Please check and give me an answer for the following error.
The section of the code which is giving the error has been included.
%read files into 3 Dim variable: data
% Dim 1 - station number
% Dim 2 - 1:3 is eta, u, v
% Dim 3 - time steps
for numm = 1:nsta
FILE1 = fopen(sprintf('D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1\sta_%04d',numm),'r');
data1 = fscanf(FILE1,'%f',[4 Inf]);
sides(numm,:,start1:stop1) = data1([4 3 2],start1:stop1);
fclose(FILE1);
disp(sprintf('File %d has been read...',numm))
end
disp('All files read')
  댓글 수: 2
Stephen23
Stephen23 2024년 1월 11일
편집: Stephen23 2024년 1월 11일
WARNING:
Both the question and the accepted answer have the same problem: they use escape characters in the SPRINTF format string without considering what effect they have. Depending on which character is escaped, this may either generate a (special) character or give a warning and not return any character, e.g.:
sprintf('\M')
Warning: Escaped character '\M' is not valid. See 'doc sprintf' for supported special characters.
ans = 1×0 empty char array
The robust solution is to NOT place file separator characters in the format string, e.g.:
P = 'D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1';
F = fullfile(P,sprintf('sta_%04d.txt',7))
F = 'D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1/sta_0007.txt'
幸一
幸一 2024년 1월 11일
i tried this step and it is not working.
I extremely sorry for disturbing to you all.

댓글을 달려면 로그인하십시오.

채택된 답변

VINAYAK LUHA
VINAYAK LUHA 2024년 1월 10일
편집: VINAYAK LUHA 2024년 1월 11일
Change line 62 with
FILE1 = fopen(sprintf('D:\\Mejena_Case\\FUNWAVE_Simulation\\mejena_tsunami\\work\\output1\\sta_%04d.txt',numm),'r');
  댓글 수: 18
Walter Roberson
Walter Roberson 2024년 1월 16일
The code has
neast = 0;
nwest = 131;
nsouth = 0;
nnorth = 0;
nsta = neast+nwest+nsouth+nnorth;
so nsta is 131
The code has
for numm = 1:nsta
FILE1 = fopen(sprintf('D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1\sta_%04d',numm),'r');
so the code will try to open up to sta_0131
However, your directory only has up to sta_0048 stored in it.
幸一
幸一 2024년 1월 17일
hello sir.
thank you very much for your kind reply.
I will recheck the code again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by