Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier.
Show older comments
Hello, I have a problem, every program I try to run gives me this error. Even with the matlab examples.
This is the code from the mathworks page:
A = magic(4);
fileID = fopen('myfile.txt','w');
nbytes = fprintf(fileID,'%5d %5d %5d %5d\n',A);
fclose(fileID);
type('myfile.txt')
1 Comment
Benjamin Kraus
on 24 Jan 2018
Can you clarify your question?
- When you say "every program", do you mean every command you try to run in MATLAB?
- If you run those commands one at a time, do you get any other error messages?
- Are you running those commands in a directory in which you have write access?
- What is the value of fileID after calling fopen?
Accepted Answer
More Answers (2)
Walter Roberson
on 24 Jan 2018
You do not have write access to the directory you are in. Your current directory is probably a directory that MATLAB is installed in.
Change
fileID = fopen('myfile.txt','w');
to
[fileID, message] = fopen('myfile.txt','w');
if fileID < 0
error('Failed to open myfile because: %s', message);
end
1 Comment
Alex Javier
on 24 Jan 2018
Akash kumar
on 4 Jun 2021
0 votes
if you open the fopen file in his format and you run the again matlab file then it gives the error.
First:- You close the fopen file in your system . In your case it is 'myfile.txt'
second:- Now you run your code.
Note:- YOUR code will be definately run.
Thanks!
3 Comments
Walter Roberson
on 5 Jun 2021
No, this is not correct for this situation.
In MATLAB, if you fopen() the same disk file multiple times within MATLAB, you are permitted to do that. How the various instances interact is not documented. It will not give you "permission denied".
In MATLAB on Mac or Linux, if you fopen() a file that a different program has open, then in most cases you are permitted ot do that. There is a "hardened" Linux that the open might fail on, though.
In MATLAB on Windows, if you fopen() a file that a different program has open, then the results depend on how the other program opened the file. By default, if simple methods were used to open the file in the other process, then the file will be locked for opening inside MATLAB. However, the fopen() will not give back "permission denied": instead the file is "busy"
"permission denied" as an error message occurs in a few situations in MS Windows:
- the file already exists, but the protections for the file are set in a way that do not permit you to open the file for the mode you are requesting (e.g., you might have read access to the file but not write access.)
- the file does not already exist, but the protections for the containing directory are set in a way that do not permit you to create a new file in the directory
- regardless of the permission and ownership of the file, if the file is underneath one of MS Windows two main directories for installed executables, then write access to the file will be denied unless you are running as Administrator.
The last of those is the most common situation that people encounter: MATLAB on Windows tends to start up the first time with its initial directory set to the main directory that MATLAB was installed into, which is under one of the places that MS WIndows denies user access to.
Akash kumar
on 5 Jun 2021
But, if you open the matlab on windows then if you open the excel data sheet which is generated by the current running code. And you try to again run the same code then it will give the "Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier." Error.
Walter Roberson
on 5 Jun 2021
But it will not be a "permission denied" situation. The message will be different. See https://www.mathworks.com/matlabcentral/answers/378848-error-using-fprintf-invalid-file-identifier-use-fopen-to-generate-a-valid-file-identifier#answer_301617 for how to get the message.
And when you cannot open a file because it is open for writing in Excel, then fclose() of the file in your MATLAB session does not help: you would need to convince Excel to close the file.
Categories
Find more on Low-Level File I/O 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!