I used a "for" loop in my M.file codes and I used "fopen" and "fclose" functions in "for" loop.
After 508 iterations, MATLAB has been stopped and following message showed:
"??? Error using ==> fprintf Invalid file identifier. Use fopen to generate a valid file identifier."
I think, MATLAB faced Low memory problem because by closing software and starting again MATLAB can continue to remain iterations. Therfore I need to a function to increase MATLAB memory. The "clear all" is not useful.
No products are associated with this question.
You aren't using the same filename are you? So that it might be a timing issue. Try adding a pause(0.1). Another shot in the dark: try fclose('all').
The best method to increase memory is installing more RAM and using a 64 bit version of Matlab.
But in your case the error message is unequivocally an invalid file identifier. This is a completely other problem and not related to memory. It means that you have tried to open a not existing file. This should be checked in every case:
FID = fopen(FileName, 'r'); if FID == -1 error('Cannot open file for reading: [%s]', FileName); end
The square brackets in the message helped me to recognize unexpected file names like control characters or spaces.
It is strongly recommended to include the path in the file name, because the current directory can change for unexpected reasons, e.g. GUI or TIMER callbacks.
Another problem can be that the closing of the files is not successful. Check this by:
list = fopen('all')
The list of files, which can be open at the same time, is limited by the operating system. Usually the need to have more than 20 file opened is a clear argument for a bad program design.
When you call
do you check whether fid=-1? E.g.,
if fid==-1 keyboard end
You need to do so, to be sure MATLAB has found the file. If you run with the above, the code will pause when it can't find the file and you can investigate why from the K>> prompt.
A few questions:
The reason for these questions is that most operating systems limit the number files (file descriptors) that can be opened by a user/process/operating system at any given time.