MATLAB Error using getImageFromFile

1 vue (au cours des 30 derniers jours)
Ryan
Ryan le 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):

Réponse acceptée

Jan
Jan le 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 commentaire
Ryan
Ryan le 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 ...)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by