iteratively plot in multiple figures with subplots

11 ビュー (過去 30 日間)
dleal
dleal 2022 年 5 月 15 日
コメント済み: dleal 2022 年 5 月 15 日
Hi all,
I want to create 2 figures, each with 2 embedded subplots. Then, I would like to alternatively add points to each of the subplots using an animatedline. I will store figure, ax, and animatedlines in a struct.
First, I created a struct object to contain figure, ax, and animatedline handles. But immediately I realize that something is wrong because the
S = struct;
% create figure objs
S.f1.fig = figure;
S.f2.fig = figure;
for jj = 1:numel(fieldnames(S))
nameFig = strcat('f', num2str(jj));
for kk = 1:2
nameAx = strcat('ax', num2str(kk));
nameAn = strcat('an', num2str(kk));
% select figure jj
fh = findobj( 'Type', 'Figure', 'Number', jj );
fh;
% create ax and animated line
S.(nameFig).(nameAx) = subplot(1,2,kk);
S.(nameFig).(nameAn) = animatedline( S.(nameFig).(nameAx) );
end
end
>> S.f1
ans =
struct with fields:
fig: [1×1 Figure]
ax1: [1×1 Axes]
an1: [1×1 AnimatedLine]
ax2: [1×1 Axes]
an2: [1×1 AnimatedLine]
>> S.f2
ans =
struct with fields:
fig: [1×1 Figure]
ax1: [1×1 Axes]
an1: [1×1 AnimatedLine]
ax2: [1×1 Axes]
an2: [1×1 AnimatedLine]
But immediately I realize that something is wrong because the parent of ax1 is Figure 2, although it should be Figure 1:
>> S.f1.ax1.Parent
ans =
Figure (2) with properties:
Number: 2
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [476 446 560 420]
Units: 'pixels'
After creating the figure Struct, I would like to plot on the subplots using a loop ( since I am streaming realtime data), for some hypothetical, realtime XNew yNew:
while true
for jj = 1:2
for kk = 1:2
nameFig = strcat('f', num2str(jj));
nameAn = strcat('an', num2str(kk));
addpoints( S.(nameFig).(nameAn), xNew, yNew) % for some hypothetical, realtime XNew yNew
end
end
drawnow;
end
How can I plot iteratively into subplots in different figures? thank you!

採用された回答

Voss
Voss 2022 年 5 月 15 日
You are not actually instructing subplot to create axes in a specific figure, so the axes go into the current figure, which is the last one created, Figure 2.
S = struct;
% create figure objs
S.f1.fig = figure;
S.f2.fig = figure;
for jj = 1:numel(fieldnames(S))
nameFig = strcat('f', num2str(jj));
for kk = 1:2
nameAx = strcat('ax', num2str(kk));
nameAn = strcat('an', num2str(kk));
% findobj finds and returns the figure's handle, but it
% does not make it the current figure
fh = findobj( 'Type', 'Figure', 'Number', jj );
% fh; % this does nothing
figure(fh); % this makes fh the current figure
% create ax and animated line
S.(nameFig).(nameAx) = subplot(1,2,kk);
S.(nameFig).(nameAn) = animatedline( S.(nameFig).(nameAx) );
end
end
S.f1.ax1.Parent == S.f1.fig % now ax1's Parent is Figure 1
ans = logical
1
  3 件のコメント
Voss
Voss 2022 年 5 月 15 日
編集済み: Voss 2022 年 5 月 15 日
You're welcome!
Your idea to store the objects in a struct is a good one. However, rather than calling the fields 'fig1', 'fig2', 'ax1', 'ax2', etc., you may consider using a struct array, each element of which is a struct that has fields 'fig', 'ax', 'an', where 'ax' is an array of axes (in this case 1-by-2) and 'an' is an array of animatedlines (also 1-by-2). This way you can avoid having to build the names (fig1, fig2, ax1, etc.) all the time, and instead use indexing into S and S(jj).fig, S(jj).ax, etc.
This approach allows the syntax to be much cleaner:
S = struct();
nFig = 2;
nAx = 2;
for jj = 1:nFig
% create a new figure, store it in S:
S(jj).fig = figure();
for kk = 1:nAx
% create axes
S(jj).ax(kk) = subplot(1,nAx,kk);
% create animated line
S(jj).an(kk) = animatedline( S(jj).ax(kk) );
end
end
S(1).ax(1).Parent == S(1).fig
ans = logical
1
S
S = 1×2 struct array with fields:
fig ax an
S(1)
ans = struct with fields:
fig: [1×1 Figure] ax: [1×2 Axes] an: [1×2 AnimatedLine]
dleal
dleal 2022 年 5 月 15 日
oh this is way better! thank you again, I will use your approch instead of what I currently have. Thanks again!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by