MATLAB Answers

Appdesigner window ends up in background after uigetfile

572 ビュー (過去 30 日間)
Fredrik Wallén
Fredrik Wallén 2016 年 7 月 19 日
編集済み: Mario Malic 2020 年 9 月 12 日
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 件のコメント
Chris McRaven
Chris McRaven 2017 年 6 月 16 日
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?



Friedrich 2019 年 9 月 6 日
編集済み: Friedrich 2019 年 9 月 6 日
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
  4 件のコメント
Mario Malic
Mario Malic 2020 年 9 月 12 日
The answer works for me, but my window flickers for a moment Which is a little bit annoying. It seems that after exiting uigetdir command window gets focused by default. Did anyone experience this?
Note: App window flickers if MATLAB main window and App are on the same monitor, and MATLAB window not minimised..


その他の回答 (6 件)

Chris McRaven
Chris McRaven 2017 年 6 月 16 日
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 件のコメント
Phil 2019 年 9 月 4 日
Thanks alot!!


Tomas Åhlman
Tomas Åhlman 2018 年 6 月 12 日
Try using:
filename = uigetfile(...); figure(app.UIFigure);
(assuming UIFigure is the name of your main window)
  3 件のコメント
Shaul Shvimmer
Shaul Shvimmer 2020 年 3 月 4 日
Worked for me as well, thank you!
(MATLAB 2019b)


WTxnitz 2020 年 4 月 20 日
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.
  2 件のコメント
WTxnitz 2020 年 5 月 27 日
Good answer. I'll give it a try.
Alternately, I use the figure title and a text box to replace the title argument macOS doen't honor.
If you play around, you can size and position the figure as a backdrop to the dialog.
I even roughly compensate for different screen resolutions.


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

Riyadh Abbas
Riyadh Abbas 2017 年 5 月 26 日
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.
  4 件のコメント
James Ryan
James Ryan 2017 年 12 月 6 日
Perhaps you misread the question. This is not a hang (as in your link). The window simply end up behind others.


awezmm 2018 年 11 月 9 日
Write commandwindow(); before you uigetdir

Community Treasure Hunt

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

Start Hunting!

Translated by