How to keep Matlab from stealing focus

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

And it is also the case that the main window can not be closed (for more than a few seconds) because anytime anything in the main window is updated it steals focus. This is totally wrong. Can't use computer when running long jobs.
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.
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

@John Signorotti answer moved here:
"Ameer, Thanks for the reply. This solution would be fine if I didn't need to visualize the data while the process is running. If I do want to see the plot, is there another solution to keeping the plots from stealing focus? I will certainly try your solution.
Thanks again John"
Ameer Hamza
Ameer Hamza on 3 May 2018
Edited: Ameer Hamza on 3 May 2018
figure window will only steal focus if a new window is created. Are you creating several figure windows?
I'm not sure I understand what you mean by "near window". I have to process many hundred of data files and review the plots periodically. Typically I have 6-8 figures open, plot different types of data on each, then reuse the same figures for subsequent iterations. I don't close them and then create new ones.
Sorry! I meant to say "new window". If you just have 6-8 figures then after creation, the subsequent call to plot() or other graphics function on those figure window should not steal the focus. Here is a simple example,
  • first run these lines to create figures, axes and obtain their handle. this process will steal focus.
f1 = figure;
f2 = figure;
ax1 = axes(f1);
ax2 = axes(f2);
hold(ax1)
hold(ax2)
  • Now call following plotting command, they will run without stealing focus, in the background.
plot(ax1, 1:10)
plot(ax1, (1:10).^2)
plot(ax2, sin(1:10))
plot(ax2, sin(1:10).^2)
I create all the figure on the first iteration through my script, which is running in a loop over the number of files I have to process. So yes I am creating figures, but only once. After that, I use the same figures for subsequent iteration. But every time something gets plotted, the figure steals focus.
are you calling figure() like this in your code
figure(1) % or figure(f1)
this will also cause the figure to focus
I think I may have found the issue. I use the figure(N), where N is the figure number, prior to plotting the data. Because I plot data from different functions, figures from one function are out of scope when I plot from a different function? So I suppose I need to create the figures as you describe above and make those object global. I believe this is what is stealing focus.
Yep, that is the issue then. Thanks so much. I'll have to try making the figure variable global so I don't have to call figure()
You are welcome.
But this fully represses the figure from appearing. In my earlier matlab versions it was possible to have the figure appearing, but not stealing focus from non-Matlab windows. E.g. I run a long loop and a figure shows me at the end of each iteration how things stand, which I can see on one screen, while I am able to type a document on the other screen from which the focus is not stolen by the new figure appearing in Matlab. How to do this? I guess it is not a command but must be somewhere in the settings.
This is certainly not an acceptable answer ... MATLAB should not be stealing Windows Focus ... period.
Windows has a very nice feature that other apps use which lights up the icon on the taskbar when something happens that the user might be interested in. MATLAB should use this feature to notify that there is a new figure.
I don't care to add a parameter to all my code which is conditionally setting the visibility property when I don't want focus stolen. FWIW, I'm submitting a bug report.
I too want to work on one screen editing my scripts while analysis plots are coming up on another screen but the figures keep stealing the focus and I need to keep cliking back in the editor window.
Does anyone have a solution to this problem?
Can you show your code?
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 (3)

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.

Stephen
Stephen on 29 Jan 2025
Edited: Stephen on 29 Jan 2025
None of these solutions work for me because they always make the plot visible at some point, which steals focus. The solution proposed here, however, does work for me.
When I create my figure, I create it with visibility off:
figure('Visible', 'off');
I then immediately create a CreateFcn callback function that will be called when the figure is opened:
set(gcf,'CreateFcn','set(gcf,''Visible'',''on'')')
This callback function sets the figure to be visible when it is created (i.e. opened).
Then I do my plot stuff and save:
plot(rand(1,10))
savefig('visibletestfig.fig');
When I open the plot in MATLAB or from Windows Explorer, the plot is visible.
The whole thing looks like this:
figure('Visible', 'off');
set(gcf,'CreateFcn','set(gcf,''Visible'',''on'')')
plot(rand(1,10))
savefig('visibletestfig.fig');
I like this because the only mods I had to make to my already running code was to add 'Visible', 'off' when I create my figure then add the following line of code that creates the callback.
And perhaps it's more robust to refer to the figure by its handle:
h = figure('Visible', 'off');
set(h,'CreateFcn','set(gcf,''Visible'',''on'')')
plot(rand(1,10))
savefig('visibletestfig.fig');
Hope this helps!
Addy
Addy on 20 Feb 2026 at 11:35
I found a simple trick. I am currently using matlab 2024a. I kept ketting this problem. In windows, I pressed Windows key + Tab. This brings up the the windows virtual desktop feature. I let MATLAB update the figure couple of times. after that it did not steal focus anymore. Instead of this Win+Tab), just the windows key (to open start menu) also works. Not sure if it works for others.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Asked:

on 3 May 2018

Answered:

on 20 Feb 2026 at 11:35

Community Treasure Hunt

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

Start Hunting!