I have a MATLAB script that I could have sworn worked fine the last time I used it (a year ago). Now, I get this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
If I understand correctly, it is failing to find or open(?) a file specified elsewhere in the script. Is this right? If so, what could cause it?
For my situation, I have checked everything, but missed an easy step.
Please select "browse your folder" and browse for your current document location before you run your 'fopen' code.
I solved this problem for my self by adding permission option to fopen. As you see in http://www.mathworks.se/help/matlab/ref/fopen.html , fopen syntax is:
Possible permissions, for example are: 'r' (default) | 'w' | 'a' | 'r+' | 'w+' | 'a+' | ...
'r' – Open file for reading.
'w' – Open or create new file for writing. Discard existing contents, if any.
'a' – Open or create new file for writing. Append data to the end of the file.
'r+' – Open file for reading and writing.
'w+' – Open or create new file for reading and writing. Discard existing contents, if any.
'a+' – Open or create new file for reading and writing. Append data to the end of the file.
...
If I use fopen without permission option, or if I use 'r' (default) option, fopen will return -1, which is error. I success with this:
I had this problem. It turned out that the file I was trying to write was too large (I didn't have enough free space to accommodate it). However, the program didn't fail until the call to fclose. Very confusing!
Try freeing up some space, or writing a very small file, to test this diagnosis.
I had the file opened in excel and as a result fopen returned a -1. Took me forever to find such a trivial problem.
It also happens when trying to create a file in a non-existent directory. Try
mkdir('folderName')
within MATLAB or just create the directory beforehand.The path with a forward slash at the beginning can cause the same error.
throws this error, while
does not produce an error.