uigetfile does not work in a loop

3 views (last 30 days)
Alan Hoskins
Alan Hoskins on 8 Jan 2013
Answered: Andrew Reibold on 5 Aug 2014
I am running the code below to generate a list of files separated by spaces. If I put a break point in the while loop or step into the loop using a break point outside the loop, everything works correctly. If I run the code normally by calling the function it is in, it never opens the uigetfile window. I have put a disp('Hi') in the loop after the uigetfile command, at it does display the text in the command window so the loop is running. How can I get this to work without stepping through it?
RelatedFiles='N/A';
FileName=1;
while FileName
[FileName, PathName]=uigetfile('*.*','Select Related Test Files (Press Cancel when Finished)','MultiSelect','on');
if ischar(FileName)
FileName={FileName};
end
if iscell(FileName)
for I=1:length(FileName)
if strcmp(RelatedFiles,'N/A')
RelatedFiles=[PathName,FileName{I}];
else
RelatedFiles=[RelatedFiles,', ',PathName,FileName{I}];
end
end
FileName=1;
end
end
  3 Comments
Jan
Jan on 9 Jan 2013
Are there any EVALs in your code, which redefine built-in functions? In debug mode, variables created by EVAL are handled differently than in non-debug mode.

Sign in to comment.

Answers (5)

Image Analyst
Image Analyst on 8 Jan 2013
Edited: Image Analyst on 8 Jan 2013
It works as expected for me in R2012b. Put "echo on" at the start of your code, and take off the semicolons. Then examine the command window and see if it did every line. And if you have disp('hi') in the loop, does it just print that millions of times as it goes blasting through the loop not waiting for uigetdir()?

Alan Hoskins
Alan Hoskins on 8 Jan 2013
Hmmm, the bug seems to be related to a function that I call before the attached code. When I just put a function wrapper around this code, it works fine. I then inserted my other function call just before the loop and it stopped working again. I then tried to insert the echo 'on' command as you suggested and it works again, though interestingly it makes Matlab lose focus. By lose focus, I mean my Matlab window (command window / editor / workspace / etc...) goes into the background behind my chrome browser window, explorer window, etc.. and I need to alt-tab or re selected in the windows task bar.
I need to play with it some more to find the actual sequence that causes the issue. It is not as simple as I first thought. Since it works in my R2011b copy of Matlab and since adding echo 'on' also makes it work, I have two workarounds. If I discover what sequence actually causes the problem I will post again.
  3 Comments
Image Analyst
Image Analyst on 8 Jan 2013
Weird that echo on fixes it. Are you going to answer my question about "hi"?

Sign in to comment.


Alan Hoskins
Alan Hoskins on 9 Jan 2013
Well, I don't know exactly why it did what it did and why it only did it in R2012b, but I figured out how to fix it. It seems that the problem was caused by me not having an end in the previously called function. The previously called function opens a figure (with uicontrols including a OK/Cancel button pair) and has as part of the code uiwait(h_figure). When one the buttons is pressed, it has at part of its callback uiresume(h_figure) and then it finishes running the code and deletes the figure. I added the end to the function just to clean it up (I was planning on posting it here) and now everything works.
Thanks for the assistance.

Daniel
Daniel on 5 Aug 2014
I ran into the same issue with uigetfile in R2012b. I fixed it by making a call to drawnow just before the call to uigetfile. ex:
drawnow;
[fn,pn] = uigetfile('*.mat','Find file');
Hope this helps!

Andrew Reibold
Andrew Reibold on 5 Aug 2014
Hi, I had this same issue before.
Sometimes matlab runs faster than figures can populate and it moves on to steps too quickly.
I can't remember if its before or after, but try putting like a 10ms pause before or after your uigetfile.
You won't notice the difference, and it will possibly solve your problem.
pause(.1)

Community Treasure Hunt

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

Start Hunting!

Translated by