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