What causes an invalid file identifier in MATLAB?

前端 未结 11 2067
余生分开走
余生分开走 2020-12-06 13:21

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         


        
相关标签:
11条回答
  • 2020-12-06 14:03

    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.

    0 讨论(0)
  • 2020-12-06 14:04

    I had the file opened in excel and as a result fopen returned a -1. Took me forever to find such a trivial problem.

    0 讨论(0)
  • 2020-12-06 14:08

    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.

    0 讨论(0)
  • 2020-12-06 14:11

    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.

    0 讨论(0)
  • 2020-12-06 14:19

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

    0 讨论(0)
提交回复
热议问题