What causes an invalid file identifier in MATLAB?

前端 未结 11 2122
余生分开走
余生分开走 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: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');
    

提交回复
热议问题