Clear Filters
Clear Filters

How to keep Matlab from stealing focus

98 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 1 May 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.
Huub Bakker
Huub Bakker on 12 Jul 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.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 3 May 2018
Edited: Ameer Hamza on 3 May 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 Comments
Walter Roberson
Walter Roberson on 1 Jul 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.

Sign in to comment.

More Answers (1)

Aastav Sen
Aastav Sen on 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 Comments
Dale Fried
Dale Fried on 11 Feb 2023
Edited: Dale Fried on 11 Feb 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)"

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!