- The large number of global variables is critical. I recommend to avoid globals in general. But this does not matter your problem here.
- myslash is easier created by the builtin function filesep.
- for j = 1 ?! Simply omit a loop, which runs once only.
- eval (['[' answer{1} ']']) is cruel. But it does not concern your problem here.
- In the subfunction cb the file is not closed, if get(h, 'Value') == lastVal. Better move the fclose(fid) to the end of the subfunction. <== This matters
MATLAB Error using getImageFromFile
1 view (last 30 days)
Show older comments
I have a MATLAB script that pulls random images from a file and displays them on screen along with a slider uicontrol, and the user is asked to rate the similarity of the two images. I have a very large set of pictures, and it necessitates about 500 trials or so per user.
However, MATLAB causes an error, but only after a couple hundred trials, and I can't figure out what is causing it. This is the error it gives me:
??? Error using ==> getImageFromFile at 38
Could not read this file: "pictures/plier.png"
Error in ==> imageDisplayParseInputs at 147
Error in ==> imshow at 199
Error in ==> semjudge>NextTrial at 243
??? Error while evaluating uicontrol Callback
The desktop configuration was not saved successfully
The desktop configuration was not saved successfully
The desktop configuration was not saved successfully
The desktop configuration was not saved successfully
The desktop configuration was not saved successfully
The desktop configuration was not saved successfully
The desktop configuration was not saved successfully
Error occurred while loading file contents from disk:
/Users/slevclab/Documents/MATLAB/Semjudge/subject_1_data(1).txt (Too many open files)
Error occurred while loading file contents from disk:
/Users/slevclab/Documents/MATLAB/Semjudge/subject_1_data(1).txt (Too many open files)
I don't know what the problem is. There is nothing wrong with the image file itself. In fact, that image had already appeared earlier in the experiment before the error message ran. This only happens after a large number of trials have been run. If I run a smaller number of trials there is no error.
The code for my script is here (it is too long to just post):
0 Comments
Accepted Answer
Jan
on 12 Mar 2012
The magic message is "Too many open files". You forgot to close the files after accessing them. But the operating system can keep a limited list of open files only.
Some comments to your code:
A helpful strategy: Always use fclose in the same function and indentation level than fopen. Never return from the function in between, e.g. by return or error. In case of doubts use onCleanup to close the file. Always check fid==-1 after fopen.
To see a list of open files:
fopen('all')
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!