Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes an invalid file identifier in MATLAB?

Tags:

matlab

fopen

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?

like image 510
nikipedia Avatar asked May 15 '12 18:05

nikipedia


People also ask

What is a file identifier in Matlab?

fid is a scalar MATLAB integer, called a file identifier. You use the fid as the first argument to other file input/output routines. If fopen cannot open the file, it returns -1 . Two file identifiers are automatically available and need not be opened. They are fid=1 (standard output) and fid=2 (standard error).

What does file identifier mean?

A file identifier is for the party preferences for handling of payment files. A file identifier is always mapped to a specific single payment file template.

What does a negative file identifier mean?

File descriptors typically have non-negative integer values, with negative values being reserved to indicate "no value" or error conditions.

How do I open a Matlab file?

On the Home tab, in the File section, click Open , and then select a file to open. You also can double-click the file in the Current Folder browser.


2 Answers

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.

like image 111
nrz Avatar answered Sep 22 '22 14:09

nrz


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');
like image 28
Mindaugas Avatar answered Sep 20 '22 14:09

Mindaugas