How to keep Matlab from stealing focus
    35 views (last 30 days)
  
       Show older comments
    
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
      
      
 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.
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
 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. 
Accepted Answer
  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
      
      
 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.
More Answers (2)
  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
 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)"
  Walter Roberson
      
      
 on 11 Feb 2023
				... As discussed a few months before that, https://www.mathworks.com/matlabcentral/answers/398905-how-to-keep-matlab-from-stealing-focus#comment_920062 
  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!
0 Comments
See Also
Categories
				Find more on Function Creation 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!









