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?
fid
(file identifier) is the output of fopen
. It's an integer, but not related to the file permanently. You need to use fopen
to get the fid
. It seems to me that you are using incorrect fid
(file identifier) in some file-related I/O command, such as fread
, fscanf
or fclose
. Unsuccessful fopen
gives fid
of -1
. For any valid normal file successful fopen
will give fid
that is 3
or greater integer.
However, without any code it's impossible to say where or what the bug or error is. You could use MATLAB debugger to single-step the code from relevant fopen
(set breakpoint there and run your program) until the relevant fclose
and see if fid
(or whatever variable name you use for file identifier) or any data structure for your file identifiers (if have more than one file identifier in your code) changes in any point between relevant fopen
and fclose
.
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:
fileID = fopen(filename,permission)
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:
fid=fopen('tmp.txt', 'w');
fid=fopen('tmp.txt', 'a');
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 encountered the same problem when trying to open ASF toolbox demos. Running Matlab as an administrator(right-click to open) seemed to solve this issue for me.
fopen
can fail because MATLAB doesn't have the permissions to read/write the file you've specified.
Try opening a file in a location where you/MATLAB have all the rights (depending on your OS).
I have used fopen
with permission and the same error came out. However, I started MATLAB as admin and that took care of the problem.
I had the file opened in excel and as a result fopen returned a -1.
Took me forever to find such a trivial problem.
The path with a forward slash at the beginning can cause the same error.
filename = '/data/myfile.txt';
throws this error, while
filename = 'data/myfile.txt';
does not produce an error.
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.
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.
It also occurs when a script is trying to read beyond the end of the file.