Error using fscanf Invalid file identifier.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
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
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
幸一
2024년 1월 10일
thank you very much for your kind reply.
I will check it and let you know.
幸一
2024년 1월 10일
these are not text files.
output save like 'sta_0001' no file extension.
Your suggestion is not working.
幸一
2024년 1월 10일
Error using fscanf Invalid file identifier. Use fopen to generate a valid file identifier.
Error in couplerFNW_NHW (line 63) data1 = fscanf(FILE1,'%f',[4 Inf]);
still same error
Hi Again.
Apparently your "fopen" function is unable to open the said file, either the path is incorrect or you don't have the read permissions. Try this out once and let me know if it doesn't work.
FILE1 = fopen(sprintf('D:\\Mejena_Case\\FUNWAVE_Simulation\\mejena_tsunami\\work\\output1\\sta_%04d',numm),'r');
幸一
2024년 1월 11일
Hello,
sorry for disturbing. I once tried your suggesstion again. now code has verified about the number of files. but again following error appeared and any output file did not make.
Error using fscanf Invalid file identifier. Use fopen to generate a valid file identifier.
Error in couplerFNW_NHW (line 63)
data1 = fscanf(FILE1,'%f',[4 Inf]);

filename = "D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1\sta_" + compose('%04d', numm);
[FILE1, msg] = fopen(filename,'r');
if FILE1 < 0
error('failed to open file "%s" because "%s"', filename, msg);
end
幸一
2024년 1월 11일
Thank you very much sir.
I will make the suggessted changes and look what will happen.
幸一
2024년 1월 11일
i checked then again giving an error like this.
Error using couplerFNW_NHW (line 67) failed to open file
"D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output_mejena1\sta_0049"
because "No such file or directory"
I uploaded all the files to this thread. please check whether giving an output according to the code.
@幸一: your data file has the file extension .txt
Neither your code nor the code given by @VINAYAK LUHA specify the file extension (so the filename you generate does not exist). If your file has a file extension then you need to specify it, e.g. as I showed you here:
幸一
2024년 1월 11일
dear sir,
data file does not have a file extension. I attached the xipped folder for this thread.
please check if possible and let me know a solution.
I tried all the suggesstions raised and nothig is working right now.
@幸一: please show the complete output of these commands:
S = dir('D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1\sta*')
{S.names}
[S.isdir]
幸一
2024년 1월 16일
not working the above command.
Following is the error message
Unrecognized field name "names". Error in untitled (line 2)
{S.names};
Try
S = dir('D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1\sta*')
{S.name}
[S.isdir]
幸一
2024년 1월 16일
hello sir,
this is the output of the above given code
ans =
0×0 empty cell array
ans =
[]
Your files are not in D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1
Note: the zip result shows the files as being in a folder named stationResults but that folder is not part of the path .
幸一
2024년 1월 16일
please sory sir. I made a mistake. this is the output
ans =
1×48 cell array
Columns 1 through 6
{'sta_0001'} {'sta_0002'} {'sta_0003'} {'sta_0004'} {'sta_0005'} {'sta_0006'}
Columns 7 through 12
{'sta_0007'} {'sta_0008'} {'sta_0009'} {'sta_0010'} {'sta_0011'} {'sta_0012'}
Columns 13 through 18
{'sta_0013'} {'sta_0014'} {'sta_0015'} {'sta_0016'} {'sta_0017'} {'sta_0018'}
Columns 19 through 24
{'sta_0019'} {'sta_0020'} {'sta_0021'} {'sta_0022'} {'sta_0023'} {'sta_0024'}
Columns 25 through 30
{'sta_0025'} {'sta_0026'} {'sta_0027'} {'sta_0028'} {'sta_0029'} {'sta_0030'}
Columns 31 through 36
{'sta_0031'} {'sta_0032'} {'sta_0033'} {'sta_0034'} {'sta_0035'} {'sta_0036'}
Columns 37 through 42
{'sta_0037'} {'sta_0038'} {'sta_0039'} {'sta_0040'} {'sta_0041'} {'sta_0042'}
Columns 43 through 48
{'sta_0043'} {'sta_0044'} {'sta_0045'} {'sta_0046'} {'sta_0047'} {'sta_0048'}
ans =
1×48 logical array
Columns 1 through 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 27 through 48
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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개)
카테고리
도움말 센터 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
제품
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
