MATLAB Answers

11

Appdesigner window ends up in background after uigetfile

Asked by Fredrik Wallén on 19 Jul 2016
Latest activity Edited by Patrick Schuster on 26 Nov 2019 at 13:31
I am creating a graphic program using appdesigner but when I call a function where the user can choose a file (uigetfile) the figure window is often put behind some other of my open windows after the file has been chosen. For example if I have a word document open at the same time as my program, the word document gets focus when the file has been chosen and I have to click at the bottom bar to get back to my program. I was using GUIDE before and then I didn't have that problem. I am running on Windows. Is there a way to solve this?

  2 Comments

Same problem here. When I call uigetfile or uiputfile from an App Designer app, the focus is not correctly transferred to the new file selector window. Any workarounds?
I am also seeing this problem. I think this is unexpected behavior. The focus should return to the uifigure which called uigetfile(). My steps to reproduce are
  1. Make a mlapp in App Designer that has a button that calls uigetfile()
  2. Run the new program from App Designer
  3. Click on the Matlab main window
  4. Click back on the new program
  5. Click the button which calls uigetfile()
  6. Select a file, and click 'open'
  7. Focus will return to the Matlab main window, not the new program as expected
This also works if I have another figure window open. That is, if I click away from the new program to any open figure window, then click back to the new program to run uigetfile(), the focus is returned to that open figure window.
I've tried calling figure(app.NewProgram) immediately after to no effect. I have also tried various drawnow calls as described below, but none worked. Has anybody found a workaround?

Sign in to comment.

6 Answers

Answer by Friedrich
on 6 Sep 2019
Edited by Friedrich
on 6 Sep 2019
 Accepted Answer

For MATLAB R2018a and newer you can use the figure command to focus your app again,
uigetfile; % Could be other dialogs here
drawnow;
figure(app.UIFigure)
For releases prior either turn the visibility off and on again or use the attached p file (change extension from .zip to .p). The usage then would be
uigetfile; % Could be other dialogs here
drawnow;
FocusUIFigure(app.UIFigure)

  1 Comment

Hello,
I cannot open the attached zip file.
Is it broken?
Regards
Patrick

Sign in to comment.


Answer by Chris McRaven on 16 Jun 2017

I believe I have a (somewhat ugly) workaround. Immediately after calling uigetfile(), simply make the main uifigure window invisible and then visible. Assuming the name of your uifigure window is 'UIFigure', add
...
filename = uigetfile(...);
app.UIFigure.Visible = 'off';
app.UIFigure.Visible = 'on';
...
right after the call to uigetfile().

  13 Comments

Worked for me 2017b, thank you!
Great idea!!worked for me 2018b.But it's so depressing we can't direct to UIfigure
Thanks alot!!

Sign in to comment.


Answer by Tomas Åhlman on 12 Jun 2018

Try using:
filename = uigetfile(...); figure(app.UIFigure);
(assuming UIFigure is the name of your main window)

  1 Comment

Sign in to comment.


Answer by Riyadh Abbas on 26 May 2017

Hi there, I found a solution to this issue provided by http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem, which suggested adding two lines drawnow; pause(0.05);
I tried it and it did work for me, hope can solve your problem.

  4 Comments

Show 1 older comment
Me neither.
Perhaps you misread the question. This is not a hang (as in your link). The window simply end up behind others.

Sign in to comment.


Answer by awezmm
on 9 Nov 2018

Write commandwindow(); before you uigetdir

  0 Comments

Sign in to comment.


Answer by Syed Hussain on 25 Jan 2019

Hi
Its is really strange behaviour
My Solution was just to use
...
filename = uigetfile(...);
app.UIFigure.Visible = 'on';
...
It removed the glichy behaviour.
Thanks

  0 Comments

Sign in to comment.