. Advertisement .
..3..
. Advertisement .
..4..
Hi everyone, I’m learning about matlab. While working, I try undefined. As a result, I get the message:
Invalid file identifier. Use fopen to generate a valid file identifier.
What can I do about the “invalid file identifier. use fopen to generate a valid file identifier” issue? Is there a better approach?
The cause:
fopen
is fail because MATLAB is not permitted to read/write on the file you have specified. Therefore, the error appears.Solution:
This error will disappear if fopen is added permissions. You can see in http://www.mathworks.se/help/matlab/ref/fopen.html , the fopen syntax is:
‘r’ (default) | ‘w’ | ‘a’ | ‘r+’ | ‘w+’ | ‘a+’ | … are some possible permissions and and below is their functions:
‘r’ – Opening a file for reading.
‘w’ – Opening or creating a new file for writing. Cutting the existing contents, if have.
‘a’ – Opening or creating a new file for writing. Adding data to the end of the file.
‘r+’ – Opening file for reading and writing.
‘w+’ – Opening or creating new file for reading and writing. Cutting existing contents, if have.
‘a+’ – Opening or creating new file for reading and writing. Adding data to the end of the file.
…
If fopen is use with no permissions, or I use ‘r’ (default) option, fopen will return -1 value. It is an error. Then I use below commands and they give me success:
fid
(file identification) isfopen
. It’s the output offopen
. It is an integer that is not directly related to the file. To getfid
, you will needfopen
. You are probably using an incorrectfid
(file identification) in a file-related I/O command such asfread
orfscanf
, orfclose
.fopen
that fails to succeed will givefid
of the-1
. Any valid file that has been successfullyfopen
will yieldfid
which is3
or higher integer.However, it is impossible to pinpoint the source of the error or bug without any code. You can use MATLAB to run the code one-by-one from
fopen
(set breakpoint and then run your program), untilfclose
. Iffid
(or any other variable you use for file identifiers) changes betweenfopen
,fclose
, or any data structure (if your code has more than one file identifier), it will be possible to see if thatfid
(or any other variable name you used for file identifiers) is affected.