Appdesigner window ends up in background after uigetfile
이전 댓글 표시
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?
댓글 수: 3
Jorg Woehl
2016년 12월 21일
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?
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
- Make a mlapp in App Designer that has a button that calls uigetfile()
- Run the new program from App Designer
- Click on the Matlab main window
- Click back on the new program
- Click the button which calls uigetfile()
- Select a file, and click 'open'
- 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?
Viresh
2025년 9월 12일
how to do this next step
채택된 답변
추가 답변 (11개)
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)
댓글 수: 9
Patrick Schuster
2019년 11월 26일
편집: Patrick Schuster
2019년 11월 26일
Hello,
I cannot open the attached zip file.
Is it broken?
Regards
Patrick
Payam Razavi
2020년 4월 15일
편집: Payam Razavi
2020년 4월 15일
Thanks for the answer. Please note for 2020a it worked better (without any jump to other applications) without "drawnow" command. Just added the following and it worked
figure(app.UIFigure)
Omar Alamoudi
2020년 6월 24일
This is an effective workaround, but this should not be the expected behavior right? I'm using 2020a and the dialog I'm using is inputdlg
Mario Malic
2020년 9월 12일
편집: 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..
Francois Moyroud
2021년 6월 25일
편집: Francois Moyroud
2021년 6월 25일
Even when did i minimise MATLAB window that app window flicks happen. Did you get any solution about that?
Joyce Trapp
2021년 10월 13일
In Matlab 2021b following did the trick for me:
filename = uigetfile(...);
figure(UIFigure);
(also assuming UIFigure is the name of your Figure)
Onur Ozdemir
2022년 3월 14일
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.
Bruce Rodenborn
2024년 1월 23일
편집: Bruce Rodenborn
2024년 1월 23일
Terrible answer. Staff at MATHWORKS should provide complete information in their solution. In particular, app.UIfigure is generically part of a NEW app in MATLAB, but is NOT part of a converted GUI. Your answer should have been properly commented instead of the anemic information provided.
Bruce Rodenborn
2024년 1월 23일
편집: Bruce Rodenborn
2024년 1월 23일
The solution works, but you need to find the appropriate ufigure in your ComponentBrowser. I attached an image in case you have the same problem. My code looks like:
[file_mat,dir_mat,FilterIndex]=uigetfile('*.mat','Select Data File');
drawnow; %update the figure windows
figure(app.figure) %bring the GUI back to the foreground
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
Jorg Woehl
2017년 7월 13일
Didn't work either...
Eric H.
2017년 10월 24일
Chris, Your Visible = 'off'/Visible = 'on' trick worked for me. Thanks to everyone for posting this! Eric
Philip Ziegler
2017년 11월 3일
편집: Philip Ziegler
2017년 11월 3일
Thank you so much, Chris, you helped me to solve one of my issues for the GUI I had to program for my bachelors thesis! :D
Matt J
2017년 11월 11일
Didn't work for me either...
karim botros
2018년 1월 3일
it worked ,, Thank you so much for this workaround,, i hope matlab finds a solutions for this bug
Tahoe Schrader
2018년 1월 5일
This worked but is quite ugly. Wish there was a way to set focus without the app having to effectively blink on and off.
Luke M
2018년 2월 6일
Worked for me on 2017a. I haven't tried 2017b, so I don't know if they fixed it. If not, I hope MATLAB catches this and comes up with a fix.
Sajad Mahnan
2018년 3월 29일
I'm using Matlab 2018a and this issue did not fix.
Harry Talbot
2018년 4월 26일
This works for me in 2018a.
Ghazi Mrazouk
2018년 6월 7일
Worked for me 2017a.thanks
Gijs de Vries
2018년 11월 4일
Worked for me 2017b, thank you!
xinxin wang
2019년 5월 31일
Great idea!!worked for me 2018b.But it's so depressing we can't direct to UIfigure
Phil
2019년 9월 4일
Thanks alot!!
Tomas Åhlman
2018년 6월 12일
10 개 추천
Try using:
filename = uigetfile(...); figure(app.UIFigure);
(assuming UIFigure is the name of your main window)
댓글 수: 3
Mustafa Caner Bulut
2019년 2월 25일
Thank's! It works for me
Farley Postgate
2020년 1월 3일
This workaround works for me. I just tried it with matlab 2020a...
Shaul Shvimmer
2020년 3월 4일
편집: 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
f=figure();
%bring figure to front focus
drawnow;
%the uigetfile dialog now opens properly in front focus
[xl_file,xl_path] = uigetfile('*.*');
% after user closes dialog
% delete dummy
delete(f);
%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.
댓글 수: 5
Andrew Davies
2020년 5월 27일
Thanks. First usable workaround I've found. You can hide the temporary figure with
f.Visible = 'off';
just after the first drawnow.
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.
val
2021년 12월 19일
Unfortunately, this does not work for me in 2021b :(
WTxnitz
2021년 12월 19일
Sorry to hear it doesn't work for you :-(
Just checked. It still works for me 2021b, update 1 (9.11.0.1809720) - latest
on latest MacBook Pro M1 and MacOs Monterey 12.1
also works on intel MBP
can't help more than this. hope you find a fix
WTxnitz
2021년 12월 19일
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
figure(app.your_app_here)
both seem to work
NbrOneProcastinator
2023년 1월 13일
8 개 추천
2023 and still no official fix to this issue. Matlab App Designer users have been beta testers since years now!
NbrOneProcastinator
2023년 6월 27일
6 개 추천
Seven years later and still just workarounds for this problem.
Not bad, Mathworks!
Thanks for reporting the problem.
This has been fixed in R2025a. Focus now returns to the app that called uigetfile after the dialog is closed. The fix is also available in the R2024a and R2024b beta releases only (not the R2024a and R2024b general releases).
댓글 수: 2
I'm running R2024b with update 1 some of the related issues are fixed.
e.g. hitting Enter vs mouse click left the next dialog in the background.
uigetfile() left us in the background.
However in Windows, after uigetdir(); or uiconfirm();
figure(app.MyApp); is still required to bring the app back to the front. After directory selection the whole app disappears behind Chrome, Notepad, whatever else is open. At least now figure() works. In earlier versions even that didn't do it reliably.
And BTW, when it wasn't working it was NOT consistant. Sometimes it would work properly (randomly as far as I could see.)
Adam Danz
2024년 12월 16일
Thanks for these details Gavin.
Features in the beta release will soon be part of the general release. Using an upcoming release of MATLAB on my Windows machine, I just tests Gavin's list of uigetfile, uigetdir, and uiconfirm within the callback to an app button, both with mouse and keyboard activations. All combinations returned focus to the app even when a Word files, Excel file, and notepad++ file were opened and on the desktop.
As soon as that release comes out, I will update my answer to share the news and will welcome any additional feedback.
awezmm
2018년 11월 9일
1 개 추천
Write commandwindow(); before you uigetdir
Syed Hussain
2019년 1월 25일
Hi
Its is really strange behaviour
My Solution was just to use
...
filename = uigetfile(...);
app.UIFigure.Visible = 'on';
...
It removed the glichy behaviour.
Thanks
댓글 수: 1
Jorg Woehl
2024년 5월 6일
This did not bring the app window back in focus for me (R2024a), and it also doesn't resolve the issue of the file selector window ending up behind the app window when calling uigetfile.
Riyadh Abbas
2017년 5월 26일
0 개 추천
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
Jorg Woehl
2017년 7월 13일
Didn't work for me...
Matt J
2017년 11월 11일
Me neither.
Neelakanthu Karra
2017년 11월 22일
Didn't worked for me ....
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.
Jorg Woehl
2024년 5월 14일
편집: Jorg Woehl
2024년 5월 14일
0 개 추천
I have just published a simple workaround for these focus issues, which -- as The MathWorks acknowledges -- still have "currently no official workaround". Check out my File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/165961-fixfocus, which was in part inspired by this thread.
댓글 수: 6
Dmitry
2024년 8월 26일
Works for me. Thanks!
Henry Kafeman
2024년 12월 10일
I have the same issue with a window appearing behind the App window, but I am using inputdlg() and @Jorg Woehl's fixfocus() does not work.
Does anyone have any other workarounds to help me?
Thanks
@Henry Kafeman -- which release are you using? See @Adam Danz's comment above that Mathworks thinks this is fixed in R2024a beta and the followup to that for ideas.
For various reasons I have not updated so can't test, but my opinion is that if are using R2024a (beta) or later and the issue still is present a new bug report should be filed or comment back directly to Adam.
Jorg Woehl
2024년 12월 15일
inputdlg works fine "out of the box", i.e. appears in front of the app window when called from an app callback. Henry's issue was, as it turns out, linked to calling inputdlg from the app's startupFcn, and it behaved badly as a result of it. This issue is therefore unrelated to the problem that fixfocus is addressing.
Henry Kafeman
2024년 12월 16일
@dpb I am using R2024b Update 2. As per @Jorg Woehl's clarification yesterday, my issue with inputdlg() is when I call it from the startupFcn()!
Is there any way of determining or waiting for when the rendering, etc. of the App startup has completed so that the inputdlg() appears in front of the App window?
Adam Danz
2024년 12월 16일
I've provided an update in my comment under my answer. Addtionally, I tested the R2024b beta release and confirmed that all three dialogs listed in my comment successfully returned focus on the app.
The features in beta releases are not necessarilly available in the following general release. For example, dark theme has been available for several of the recent beta release but is not yet in the general release.
카테고리
도움말 센터 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!