Error Using fscanf to open file

what should i do to resolve this problem?
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in data_proc1 (line 232)
a = fscanf(fid,'%e',inf); % write all data into a column of matrix a.

3 Commenti

%---------open the file and fetch data-------------------------------
fid = fopen(char(filename));
a = fscanf(fid,'%g',inf); % write all data into a column of matrix a.
fclose(fid);
region = reshape(a,nx,ny);
clear a
When calling fopen it is always a good idea to display the message if the file cannot be opened:
[fid,msg] = fopen(...);
assert(fid>=3,msg)
Please show us that complete message.
Totally unrelated to the question, but since I saw that in your code, the whole:
if step < 10
filename_minute = ['00000',num2str(step)];
end
if step >=10 & step < 100
filename_minute = ['0000',num2str(step)];
end
if step >=100 & step < 1000
filename_minute = ['000',num2str(step)];
end
if step >=1000 & step < 10000
filename_minute = ['00',num2str(step)];
end
if step >=10000 & step < 100000
filename_minute = ['0',num2str(step)];
end
if step >=100000
filename_minute = [num2str(step)];
end
can be replaced by just:
filename_minute = num2str(step, '%06d');

Accedi per commentare.

Risposte (2)

Monika Phadnis
Monika Phadnis il 10 Lug 2019

0 voti

The file you want to open should be in the same folder that is currently open in MATLAB for your script. You can browse to the folder where the script is. Is the file in the same folder?
Guillaume
Guillaume il 10 Lug 2019
Why the conversion of filename to char? If it's not already a char array what is it?
You should always check the return value of fopen. Clearly, it failed to open the file. The most common reason is that your current directory is not where the file is. And the easiest way to fix that is to use absolute paths
folder = 'C:\somewhere\somefolder'; %use absolute path
[fid, errmsg] = fopen(fullfile(folder, char(filename))); %why the char?
assert(fid > 0, 'Failed to open file, error was: %s', errmsg);
Also, rather than clearing the a variable, which has a meaningless name, simply reuse your region variable, which is a much better name anyway:
region = fscanf(fid,'%g',inf);
fclose(fid);
region = reshape(region, nx, ny);

6 Commenti

I have trying ur suggestion, and this is the result
Error using data_proc1 (line 233)
Failed to open file, error was: 78Failed to open file, error was: 111Failed to open file, error was:
32Failed to open file, error was: 115Failed to open file, error was: 117Failed to open file, error was:
99Failed to open file, error was: 104Failed to open file, error was: 32Failed to open file, error was:
102Failed to open file, error was: 105Failed to open file, error was: 108Failed to open file, error was:
101Failed to open file, error was: 32Failed to open file, error was: 111Failed to open file, error was:
114Failed to open file, error was: 32Failed to open file, error was: 100Failed to open file, error was:
105Failed to open file, error was: 114Failed to open file, error was: 101Failed to open file, error was:
99Failed to open file, error was: 116Failed to open file, error was: 111Failed to open file, error was:
114Failed to open file, error was: 121
Guillaume
Guillaume il 10 Lug 2019
Modificato: Guillaume il 12 Lug 2019
What OS are you using?
Which version of matlab?
Can you paste the exact code you're using? (from the folder = ... line to the fopen line)?
matlab 2014a
Please answer the other two questions, otherwise we won't be able to help you further.
Windows 8. here i attach scribt
"I have trying ur suggestion, and this is the result: Failed to open file, error was: 78"
The code you've attached is not the one that produced that result.

Accedi per commentare.

Categorie

Prodotti

Tag

Richiesto:

il 10 Lug 2019

Commentato:

il 12 Lug 2019

Community Treasure Hunt

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

Start Hunting!

Translated by