uigetfile() goes behind main figure
19 visualizzazioni (ultimi 30 giorni)
uigetfile(), I think, creates a new figure; it has no parent argument. A number of people complained that its dialog window has unexpeced bahaviour in terms of appearance and focus.
I'm calling it from my main figure (created programmatically with uifigure). The uigetfile() dialog opens as a child of MATLAB window, and NOT as a child of my GUI app which is actually calling it. I.e. it opens behind the main app (problem #1); you need to click on Matlab icon to see this dialog, then, once you select file(s) the focus remains with Matlab window, then, to come back to your app (problem #2), again, manually click on your app to give it the focus.
I found one workaround which seems failing in 2021b on Mac--the dialog still opens behind the main calling app:
f2 = figure('Visible','off'); % create a dummy figure
drawnow; % give it a focus
[file, path] = uigetfile(...); % call this stubborn thing
delete(f2); % delete dummy
% the above does not work on Mac
% then the 2nd workaround, to give focus back to the calling main fig
% it works fine
figure(f); % f is a handle of the main fig, created with uifigure()
Anybody can help to open this dialog on top of the main app?
I'm on MacOS Monterey. It could be OS dependant, I'm afraid.
The thing is: the new figure is spawned inside uigetfile(), and currently uigetfile() has no way to explicitly mention the parent, therefore, I believe it is Java/OS who decides on the ancestor. And at least on Mac it decides to open it in the main Matlab window. Maybe the dummy figure workaround works on Win (or there is no problem #1 on Win in the first place). One can work with Java window manager to change the focus, but this needs to be done right after the appearance of the dialog, i.e. inside the uigetfile(), not before or after its call.
Eventually, I want to compile the app to a standalone. Thus, there will be no Matlab main window. Maybe then it will behave better. What I'm hearing from others is that this appearance/focus behavior is not consitent between App-Design-created vs programmatical, compiled vs in-Matlab, and diff versions.
Pierre il 7 Feb 2023
Alright, so it looks like, at least with the version I'm running as of writing (R2022b), the only available solution is a brute force approach similar to that suggested for other, slightly different problems involving uigetfile() and uifigure focus. The idea is to hide your main UI window (and any other windows that the uigetfile dialog might be hiding behind), then call uigetfile(), and then make your windows visible again (h/t @Chris McRaven and others):
app.UIFigure.Visible = 'off'; % Hide the main window
[file, path] = uigetfile(); % Trigger the uigetfile dialog
app.UIFigure.Visible = 'on'; % Make the main window visible again
The main difference with the solutions to the question linked by @Prathamesh Kulkarni is that here we really need to hide the main window before calling uigetfile() since we're not just doing the Visible 'off'/'on' trick to force Matlab to snap focus back to the main window. And if your app has opened other sub-windows that the uigetfile dialog might also be hiding behind, you'll have to manually hide them as well before calling uigetfile().
I really wish there was a nicer way to do this (or simply that dialog windows auto-focused) but what can you do.
Prathamesh Kulkarni il 11 Lug 2022
I don't think it's an OS specific issue. Similar questions were asked earlier, you can refer to the answers provided to the following question. I hope it will resolve your issue.