MATLAB Error using getImageFromFile

4 views (last 30 days)
Ryan
Ryan on 12 Mar 2012
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):

Accepted Answer

Jan
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:
  • 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
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')
  1 Comment
Ryan
Ryan on 12 Mar 2012
Thanks!
Thanks for the other suggestions, too. I am pretty new to MATLAB, I just had to learn it for my new job, so my code is pretty messy and jurry-rigged.
Could you clarify what's wrong with the eval (['[' answer{1} ']']) thing and j=1?
(I don't like working with global variables either, but I don't know how else to pass the value of a variable between different functions ...)

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings 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!