What causes an invalid file identifier in MATLAB?

fopen, matlab

Solution

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

Problem

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?

Original source