Appdesigner window ends up in background after uigetfile

400 views (last 30 days)
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?
Chris McRaven
Chris McRaven on 16 Jun 2017
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.

Accepted Answer

Onur Ozdemir
Onur Ozdemir on 14 Mar 2022
Edited: Onur Ozdemir on 14 Mar 2022
As WTxnitz said, all these answers address getting the app window back as focus after uigetfile but what is preferred is to prevent it from minimizing in the first place. Here I made a small change to WTxnitz's answer, this way a non visible dummy figure is created instead of sending everything to background.
f = figure('Renderer', 'painters', 'Position', [-100 -100 0 0]); %create a dummy figure so that uigetfile doesn't minimize our GUI
[filename,path] = uigetfile('*.txt', 'Open text file','MultiSelect','on');
delete(f); %delete the dummy figure
This prevents uigetfile from sending appdesigner window to background while !
Best of luck.
Jonas on 18 Apr 2023
@Onur Ozdemir In addition, I would also suggest to set
since if the user closes the dummy window manually, the workaround will not work (at least for me in 2022a)

Sign in to comment.

More Answers (9)

Chris McRaven
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().

Sign in to comment.

Friedrich on 6 Sep 2019
Edited: Friedrich on 6 Sep 2019
For MATLAB R2018a and newer you can use the figure command to focus your app again,
uigetfile; % Could be other dialogs here
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
Onur Ozdemir
Onur Ozdemir on 14 Mar 2022
This answer correctly restores the window! but you can prevent it from minimizing too (which I prefer), check out my answer. Thanks and good luck.

Sign in to comment.

Tomas Åhlman
Tomas Åhlman on 12 Jun 2018
Try using:
filename = uigetfile(...); figure(app.UIFigure);
(assuming UIFigure is the name of your main window)

WTxnitz on 20 Apr 2020
All these answers address getting the app window back as focus after uigetfile.
The other side of the problem for me is the uigetfile dialog itself launches behind all other windows.
Here is a workaround I found. This works on 2020a and macOS Mojave
% open dummy figure
%bring figure to front focus
%the uigetfile dialog now opens properly in front focus
[xl_file,xl_path] = uigetfile('*.*');
% after user closes dialog
% delete dummy
%use solution elsewhere to return focus to app
app.your_app_here.Visible = 'off';
app.your_app_here.Visible = 'on';
This seems to work both from within the app embedded code and with "external" files
in the main Matlab window.
WTxnitz on 19 Dec 2021
Not sure what didn't work
my answer is to get the uigetfile dialog itself in front focus
I did check my code and there is one difference, as to returning focus to the app
Based on other people's input instead of:
%use solution elsewhere to return focus to app
app.your_app_here.Visible = 'off';
app.your_app_here.Visible = 'on';
I use
both seem to work

Sign in to comment.

NbrOneProcastinator on 13 Jan 2023
2023 and still no official fix to this issue. Matlab App Designer users have been beta testers since years now!

awezmm on 9 Nov 2018
Write commandwindow(); before you uigetdir

Syed Hussain
Syed Hussain on 25 Jan 2019
Its is really strange behaviour
My Solution was just to use
filename = uigetfile(...);
app.UIFigure.Visible = 'on';
It removed the glichy behaviour.

NbrOneProcastinator on 27 Jun 2023
Seven years later and still just workarounds for this problem.
Not bad, Mathworks!

Riyadh Abbas
Riyadh Abbas on 26 May 2017
Hi there, I found a solution to this issue provided by, which suggested adding two lines drawnow; pause(0.05);
I tried it and it did work for me, hope can solve your problem.
James Ryan
James Ryan on 6 Dec 2017
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.


Find more on Develop uifigure-Based Apps 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!