iteratively plot in multiple figures with subplots

5 views (last 30 days)
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!

Accepted Answer

Voss
Voss on 15 May 2022
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 Comments
dleal
dleal on 15 May 2022
oh this is way better! thank you again, I will use your approch instead of what I currently have. Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!