How can I prevent the clicked figure taking the focus?

1 回表示 (過去 30 日間)
Mahmut
Mahmut 2020 年 6 月 12 日
コメント済み: Walter Roberson 2021 年 5 月 2 日
I have created a MATLAB script in a for loop that makes calculations, create a figure and automacitally uptade this figure about 1 min time long, like that:
for n=1:5
figure(n)
for x=1:100
%calculations
%plot
%hold on
end
%hold off
end
The problem is while the figure 2 is keeping updating and I click the figure 1, the final plot on the figure 1 replaces by the figure 2 and I lose my figure 1. What should I do?
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 12 日
search for tag:always-parent for discussion

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

回答 (2 件)

Cam Salzberger
Cam Salzberger 2020 年 6 月 12 日
Hello Mahmut,
Rather than worrying about which figure is in focus (the current figure), it would be better practice to target your plot (or hold) commands to use a specific figure (or axes, in the case of plot). For example:
figHandle = figure;
axHandle = axes('Parent', figHandle, 'NextPlot', 'add');
plot(axHandle, x, y)
newFig = figure;
plot(axHandle, a, b)
-Cam
  4 件のコメント
undefined undefined
undefined undefined 2021 年 5 月 1 日
thanks !
what you mean in "traditional figures" ?
Walter Roberson
Walter Roberson 2021 年 5 月 2 日
Traditional figures are Java based and are created by figure().
There is a newer kind of figures that are based on web technologies and HTML, that are created by uifigure()

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


Voss
Voss 2020 年 6 月 12 日
The default behavior of the plot() command is to plot into the currrent axes in the current figure. Clicking a figure makes it the current figure, which is why subsequent plot commands replace what was there. To avoid this behavior, plot into specific axes (i.e., not necessarily the current axes in the current figure). Something like this might work:
for n=1:5
figure(n); % figure n is now the current figure
ax = gca(); % ax is now the current axes
for x=1:100
% calculations
% plot(ax,...) instead of plot(...)
% hold(ax,'on') instead of hold on
end
% hold(ax,'off') instead of hold off
end
By specifying ax to the plot command, you are telling MATLAB to plot to that axes, rather than the current axes (similarly for the hold command). This way you can interact with previously created figures while the code is running, and they will still become the current figure, but MATLAB will plot to the new one.
  6 件のコメント
Mahmut
Mahmut 2020 年 6 月 13 日
I do not get the error now but replacing problem still exist.
Steven Lord
Steven Lord 2020 年 6 月 14 日
Can you show your updated code?

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

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by