Appdesigner window ends up in background after uigetfile

조회 수: 319 (최근 30일)
Fredrik Wallén
Fredrik Wallén 2016년 7월 19일
댓글: sunshine 2024년 4월 17일 5:10
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
Jorg Woehl
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
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Onur Ozdemir
Onur Ozdemir 2022년 3월 14일
편집: Onur Ozdemir 2022년 3월 14일
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.
  댓글 수: 7
Adee
Adee 2024년 4월 16일 10:56
Charming!
Making the figure invisible also works - the negative location may end up on another display.
All that's needed is
f = figure('Visible', 'off', 'CloseRequestFcn', '');
sunshine
sunshine 2024년 4월 17일 5:10
Visible off not works for me

댓글을 달려면 로그인하십시오.

추가 답변 (9개)

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
xinxin wang
xinxin wang 2019년 5월 31일
Great idea!!worked for me 2018b.But it's so depressing we can't direct to UIfigure
Phil
Phil 2019년 9월 4일
Thanks alot!!

댓글을 달려면 로그인하십시오.


Friedrich
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
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
Bruce Rodenborn
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
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

댓글을 달려면 로그인하십시오.


Tomas Åhlman
Tomas Åhlman 2018년 6월 12일
Try using:
filename = uigetfile(...); figure(app.UIFigure);
(assuming UIFigure is the name of your main window)
  댓글 수: 3
Farley Postgate
Farley Postgate 2020년 1월 3일
This workaround works for me. I just tried it with matlab 2020a...
Shaul Shvimmer
Shaul Shvimmer 2020년 3월 4일
편집: Shaul Shvimmer 2020년 3월 4일
Worked for me as well, thank you!
(MATLAB 2019b)

댓글을 달려면 로그인하십시오.


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

WTxnitz
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
WTxnitz
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
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
NbrOneProcastinator 2023년 6월 27일
Seven years later and still just workarounds for this problem.
Not bad, Mathworks!

awezmm
awezmm 2018년 11월 9일
Write commandwindow(); before you uigetdir

Syed Hussain
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

Riyadh Abbas
Riyadh Abbas 2017년 5월 26일
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
Neelakanthu Karra
Neelakanthu Karra 2017년 11월 22일
Didn't worked for me ....
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by