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 Kommentare

Stephen23
Stephen23 am 11 Jan. 2024
Bearbeitet: Stephen23 am 11 Jan. 2024
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'
幸一
幸一 am 11 Jan. 2024
i tried this step and it is not working.
I extremely sorry for disturbing to you all.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

VINAYAK LUHA
VINAYAK LUHA am 10 Jan. 2024
Bearbeitet: VINAYAK LUHA am 11 Jan. 2024

0 Stimmen

Change line 62 with
FILE1 = fopen(sprintf('D:\\Mejena_Case\\FUNWAVE_Simulation\\mejena_tsunami\\work\\output1\\sta_%04d.txt',numm),'r');

18 Kommentare

幸一
幸一 am 10 Jan. 2024
thank you very much for your kind reply.
I will check it and let you know.
幸一
幸一 am 10 Jan. 2024
these are not text files.
output save like 'sta_0001' no file extension.
Your suggestion is not working.
幸一
幸一 am 10 Jan. 2024
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');
幸一
幸一 am 11 Jan. 2024
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
幸一
幸一 am 11 Jan. 2024
Thank you very much sir.
I will make the suggessted changes and look what will happen.
幸一
幸一 am 11 Jan. 2024
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.
Stephen23
Stephen23 am 11 Jan. 2024
@幸一: 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:
幸一
幸一 am 11 Jan. 2024
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.
Stephen23
Stephen23 am 11 Jan. 2024
Bearbeitet: Stephen23 am 11 Jan. 2024
@幸一: please show the complete output of these commands:
S = dir('D:\Mejena_Case\FUNWAVE_Simulation\mejena_tsunami\work\output1\sta*')
{S.names}
[S.isdir]
幸一
幸一 am 16 Jan. 2024
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]
幸一
幸一 am 16 Jan. 2024
hello sir,
this is the output of the above given code
ans =
0×0 empty cell array
ans =
[]
Walter Roberson
Walter Roberson am 16 Jan. 2024
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 .
幸一
幸一 am 16 Jan. 2024
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.
幸一
幸一 am 17 Jan. 2024
hello sir.
thank you very much for your kind reply.
I will recheck the code again.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Tags

Gefragt:

am 10 Jan. 2024

Kommentiert:

am 17 Jan. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by