How can I increase Memory

Dear friends
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.
Regards

3 Kommentare

Matt J
Matt J am 6 Jan. 2013
It doesn't sound like a memory issue. Why don't you think it's an issue with the file identifier, like the error message says?
Mohsen
Mohsen am 6 Jan. 2013
Because in previous iterations (first 508 iterations), file identifier do not have any error.
Jan
Jan am 6 Jan. 2013
Bearbeitet: Jan am 6 Jan. 2013
Btw., clear all is useful only in very strange situations.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Jan. 2013

1 Stimme

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

2 Kommentare

Jan
Jan am 6 Jan. 2013
@Mohsen: Flags are used to inform the admins and editors about a message, which should be deleted or edited because it conflicts with the forum rules. Therefore I've deleted your flag "Regards, your guide is useful".
Jan
Jan am 7 Jan. 2013
@Mohsen: Now you have flagged my comment. Does this mean, that you want the admins and other editors to check, if my comment violates the forum rules? Although the message of your flag "Thanks a lot" seems to imply, that you do not want my comment to be deleted due to rude or inappropriate contents, I hesitate to delete a flag of my own posting.
Please use comments to post comments, and not flags.
@Admins/editors: Please remove this flag. Sorry for being nitpicking, but I think that editors should not clean up flags for their own postings. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Jan
Jan am 6 Jan. 2013
Bearbeitet: Jan am 6 Jan. 2013

0 Stimmen

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.
Matt J
Matt J am 6 Jan. 2013
Bearbeitet: Matt J am 6 Jan. 2013

0 Stimmen

When you call
fid=fopen(...)
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.

3 Kommentare

Mohsen
Mohsen am 6 Jan. 2013
After error, software should be close and and start again because software has been interrupted.
Matt J
Matt J am 6 Jan. 2013
You shouldn't reach the error if you've inserted
if fid==-1
keyboard
end
before the fid is used.
Then just put
if fid == -1
continue;
end
and it will just skip that file and continue on.

Melden Sie sich an, um zu kommentieren.

Konrad Malkowski
Konrad Malkowski am 6 Jan. 2013

0 Stimmen

A few questions:
  1. What operating system are you using?
  2. Do you close the previously open files?
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.

1 Kommentar

Walter Roberson
Walter Roberson am 7 Jan. 2013
Especially with the "508" being meantioned. MATLAB uses 3 file descriptors internally, so the problem is showing up after 511 open files, which is almost certainly a system limit of some kind.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by