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

errorfprintf

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')

Best Answer

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.