How to keep Matlab from stealing focus
98 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
John Signorotti
le 3 Mai 2018
Commenté : Huub Bakker
le 12 Juil 2023
I do a lot long running scripts that plot to multiple figure. While these scripts are running, I am unable to use my computer because Matlab steals focus so I can't do anything else while the script is running. It there a way to keep the plot function from stealing focus?
3 commentaires
Walter Roberson
le 1 Mai 2021
because anytime anything in the main window is updated it steals focus.
That is not accurate in MATLAB. In MATLAB, focus can be stolen when the code executes figure() or uifigure(), or explicitly makes a figure visible.
Functions such as msgbox() use figure() and so focus can be stolen by executing them because of the figure() call.
Focus can be stolen by the command window in some cases of typing.
Focus is not stolen by plotting routines other than figure() or uifigure() . However, if a plotting routine is called at a time when there is no current figure, then one will be created to contain the graphics and that will steal focus in doing so.
For the most reliable graphics, always parent your graphics; see https://www.mathworks.com/matlabcentral/answers/317181-non-deterministic-behavior-with-multiple-figures#answer_247493
Huub Bakker
le 12 Juil 2023
I am using Matlab 2022a on an Apple Silicon Mac.
In my case, I have created a figure once and use it to replot and then print each figure to a png file. The Visible property of the figure is set to 'off' and nothing is printed to the main window.
Matlab still steals the focus when the figure run the print command. This is also true if using the exportgraphics command.
The only way I have found to stop this is to create a standalone application which I run from a Unix terminal.
Réponse acceptée
Ameer Hamza
le 3 Mai 2018
Modifié(e) : Ameer Hamza
le 3 Mai 2018
You can set the Visible property of figure handles as 'off'. It will prevent them from stealing focus.
f = figure('Visible', 'off');
at the end, when you want to see it
f.Visible = 'on'; % or set(f, 'Visible', 'on');
14 commentaires
Walter Roberson
le 1 Juil 2020
The technique that is available is to create all of the figures at the beginning, setting their visibility off. After that, do not figure() them to make them active: at most set(groot, 'CurrentFigure', ...) and better yet, only refer to them without activating them, such as ax = axes('Parent', fig(1)); plot(ax, rand(1,5));
The focus stealing would be restricted to a short period.
However, there are some cases where MATLAB does not recalculate some properties until the container is made visible, and in some cases that can end up requiring that the figure be made visible in order to trigger the recalculations. Unfortunately that can steal focus.
Plus de réponses (1)
Aastav Sen
le 23 Nov 2020
This works for me (adding data to a plot within a loop without having Matlab steal the focus so I can work on other things at the same time)! So in a nutshell:
Call before your loop:
fg = figure(1)
But during your loop when you want to update the contents of a figure this requires you to 'grab' the figure and set it to your current figure handle = 'gcf'. You do this using:
set(0,'CurrentFigure',fg);
Note: that if you instead used in your loop:
fg = figure(1)
This would set the 'gcf' to our figure (what we want), but, would also steal the focus (what we don't want)!
Hope this helps.
2 commentaires
Dale Fried
le 11 Fév 2023
Modifié(e) : Dale Fried
le 11 Fév 2023
Thanks!
I like your approach because the figures are visble and updated, so I can see them while I am working elsewhere on my screen, and monitor progress. But focus is not stolen.
In short, before the loop, call "fh = figure(1)". Then, in the loop replace "fh = figure(1)" with "set(0,'CurrentFigure', fh)"
Walter Roberson
le 11 Fév 2023
... As discussed a few months before that, https://www.mathworks.com/matlabcentral/answers/398905-how-to-keep-matlab-from-stealing-focus#comment_920062
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!