How to keep Matlab from stealing focus

67 ビュー (過去 30 日間)
John Signorotti
John Signorotti 2018 年 5 月 3 日
編集済み: Stephen 2025 年 1 月 29 日 20:48
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 1 日
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 2023 年 7 月 12 日
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.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 3 日
編集済み: Ameer Hamza 2018 年 5 月 3 日
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 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 7 月 1 日
Can you show your code?
Walter Roberson
Walter Roberson 2020 年 7 月 1 日
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.

サインインしてコメントする。

その他の回答 (2 件)

Aastav Sen
Aastav Sen 2020 年 11 月 23 日
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 件のコメント
Dale Fried
Dale Fried 2023 年 2 月 11 日
編集済み: Dale Fried 2023 年 2 月 11 日
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
Walter Roberson 2023 年 2 月 11 日

サインインしてコメントする。


Stephen
Stephen 2025 年 1 月 29 日 20:27
編集済み: Stephen 2025 年 1 月 29 日 20:48
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!

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by