Search code examples
windowsmatlabquotas

Invalid file identifier error in Matlab loop


If I run the example code below, I get an invalid file identifier error in Matlab:

for i = 1:99999
    fid = fopen('test.txt','w');
    fprintf(fid,'%s', 'Hello World!\r\n');
    fclose(fid);
    delete('test.txt');
end;

??? Error using ==> fprintf Invalid file identifier. Use fopen to generate a valid file identifier.

The interesting thing is, that if I decrease the number of loops, I don't get the error. I researched the problem, and it seems that none of the usual issues that cause the error (Wrong File Path, Corrupt File, File doesn't exist, File already in use) are the culprits, because it works if I change the loops to 10 instead of 99999.

Upon further research, Matlab Forum Post, it seems the problem might be quota related (I think quotas have to do with the OS where the OS, Windows 10 in my case doesn't allow a program to write files after a certain amount of them have been written by the same program?).

How would one increase the quota? Is there a work around? I use Matlab 2010a on Windows 10.

I have also attempted running Matlab in administrator mode with no success.


Solution

  • I'm assuming permissions are correct and disk space is not a problem, but you should check fopen's output nevertheless to get more info or some try-catch which calls ferror(fid) for additional data (note the absence of the semicolon, obviously).

    [fid,msg]=fopen('test.txt','w')
    

    If it IS quota related you should be able to disable it in your hard drive's properties, as shown in the image below (it's in spanish, but you should get the idea). Just right click in the unit and access Properties->Disk Quota->Show Configuration and disable it if it isn't already.

    GUI location of the disk quota