iteratively plot in multiple figures with subplots

4 visualizaciones (últimos 30 días)
dleal
dleal el 15 de Mayo de 2022
Comentada: dleal el 15 de Mayo de 2022
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!

Respuesta aceptada

Voss
Voss el 15 de Mayo de 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 comentarios
Voss
Voss el 15 de Mayo de 2022
Editada: Voss el 15 de Mayo de 2022
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 el 15 de Mayo de 2022
oh this is way better! thank you again, I will use your approch instead of what I currently have. Thanks again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by