Error using fprintf Invalid file identifier. Use fopen to generate a valid file identifier.

106 Ansichten (letzte 30 Tage)
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 Kommentar
Benjamin Kraus
Benjamin Kraus am 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Benjamin Kraus
Benjamin Kraus am 24 Jan. 2018
When fopen fails, it does not generate an error message. Instead, it sets the fileID to -1.
Check the value of fileID after calling fopen.
If it is -1, check whether you have write permissions in the directory you are trying to write to. Most likely you are trying to write a file to a directory that is read-only, and you either need to change directories or specify an absolute path instead of a relative path.
  3 Kommentare
Walter Roberson
Walter Roberson am 24 Jan. 2018
"How do I know if I have write permissions in the directory"
The "Permission denied" message tells you that you do not have permission.
You can check some aspects of permissions on your current folder by executing
fileattr .
where the '.' is part of the command.
If you want the details, then you can look at the directory security settings https://msdn.microsoft.com/en-us/library/bb727008.aspx
To specify an absolute pathname, give it in the fopen. For example,
[fileID, message] = fopen('C:\Users\AlJa\Documents and Settings\MATLAB\myfile.txt', 'w');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 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

Akash kumar
Akash kumar am 4 Jun. 2021
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 Kommentare
Akash kumar
Akash kumar am 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
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by