Update figure in background without stealing focus?
Show older comments
I have a MATLAB script that periodically loads data output from a external model simulation of fluid flow, and then plots (or re-plots) it. When data are plotted, the figure window becomes the active window and is brought to the front of the screen, stealing the focus from any other window (emacs, word, etc).
I would like the matlab figure to simply update in the background so I can visually track model progress while working on other things.
Thanks!
1 Comment
Jess
on 11 Jan 2021
Oh for this to be a universal preference or setting that a MATLAB user could toggle and set and forget!
Accepted Answer
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 you 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.
1 Comment
Jess
on 11 Jan 2021
That looks like something that would work if one was updating a single figure many times. (Is that correct?) If one was to make a dozen plots once each, wouldn't one have to do that a dozen times?
Sergio López-Ureña
on 19 Oct 2020
You can select the figure by number.
set(groot,'CurrentFigure',figNumber);
It does exactly the same that
figure(figNumber);
but without focusing.
It is useful when you don't want to save the figure handler.
1 Comment
Thanks Segio for this tip, I then integrated it into a wrapper function to replace the figure() function:
function fig(indFig)
% Convenient function to replace the figure(X),
% Very convenient in loop where an update of figure must be done without stealing the focus
if nargin == 0
figure() % if no argument then just creates a new figure, but steal focus
return
end
try
set(groot,'CurrentFigure',indFig); % replace figure(indFig) without stealing the focus
catch
figure(indFig) % if indFig is not an exsting figure it creates it (and steal the focus)
end
% Drawnow is here in order to force update of the figure,
% Otherwise I observed that content of figure is not updated in loop.
% Normally 'drawnow' should take place at the end of plotting
% and NOT before plotting however in loop functions it updates
% with one loop late, this prevent to write 'drawnow' everywhere in the code.
drawnow
Categories
Find more on Graphics Performance 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!