What causes an invalid file identifier in MATLAB?

前端 未结 11 2065
余生分开走
余生分开走 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 13:52

    I have used fopen with permission and the same error came out. However, I started MATLAB as admin and that took care of the problem.

    0 讨论(0)
  • 2020-12-06 13:53

    I encountered the same problem when trying to open ASF toolbox demos. Running Matlab as an administrator(right-click to open) seemed to solve this issue for me.

    0 讨论(0)
  • 2020-12-06 13:59

    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');
    
    0 讨论(0)
  • 2020-12-06 13:59

    The path with a forward slash at the beginning can cause the same error.

    filename = '/data/myfile.txt';
    

    throws this error, while

    filename = 'data/myfile.txt';
    

    does not produce an error.

    0 讨论(0)
  • 2020-12-06 13:59

    It also happens when trying to create a file in a non-existent directory. Try mkdir('folderName') within MATLAB or just create the directory beforehand.

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

    It also occurs when a script is trying to read beyond the end of the file.

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