Update figure in background without stealing focus?

조회 수: 37 (최근 30일)
Michael H.
Michael H. 2012년 4월 10일
편집: Matthieu 2023년 8월 15일
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
Jess
Jess 2021년 1월 11일
Oh for this to be a universal preference or setting that a MATLAB user could toggle and set and forget!

댓글을 달려면 로그인하십시오.

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 4월 10일
How are you updating the figure? For example this little function with a timer that updates a plot doesn't steal focus from me.
function example_focusin
T = timer('timerfcn',@updatePlot,'period',5,'executionmode','fixedrate','taskstoexecute',10);
figure;
h = plot(rand(1,10));
start(T);
function updatePlot(src,evt)
set(h,'ydata',rand(1,10));
end
end
  댓글 수: 11
Walter Roberson
Walter Roberson 2020년 2월 19일
vc being the output of viscircles has no information about where the detected circles are -- that information is returned by imfindcircles() . viscircles only has information about where the circles are drawn on the plot, not where they are in the image.
Anyhow:
  1. Before: Use hgtransform() to create a reference hg transform group object.
  2. Before: Use plot() or line() to create a single circle of radius 1 and center [0 0], parented to the above hgtransform group. Do not use viscircles() for this purpose: for whatever reason, you cannot parent a viscircles() to a hgtransform group.
  3. Before: initialize a pool of handle objects with hgobjects(0)
  4. In loop: do the detection of circles
  5. In loop: count how many circles you got back
  6. In loop: if the number of circles you got back is fewer than the number of elements in the pool, then set() the extra pool entries 'Visible', 'off' -- do not delete them, just set them off.
  7. In loop: if the number of circles you got back is more than the number of elemetns in the pool, then use copyobj() as many times as needed to create copies of the reference hgtransform group, adding them to the pool.
  8. In loop: For as many entries as there are circles detected this time around, set the corresponding hgtransform group to have a transform matrix that scales by the detected radius, and translates by the detected center.
  9. In loop: set the Visible property to 'on' for the first so-many entries in the pool.
  10. In loop: drawnow() to make the changes visible.
LeChat
LeChat 2020년 2월 20일
I'll try this out thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (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 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
Jess
Jess 2021년 1월 11일
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
Sergio López-Ureña 2020년 10월 19일
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
Matthieu
Matthieu 2023년 8월 15일
편집: Matthieu 2023년 8월 15일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by