iteratively plot in multiple figures with subplots
    조회 수: 9 (최근 30일)
  
       이전 댓글 표시
    
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!
댓글 수: 0
채택된 답변
  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
댓글 수: 3
  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
S
S(1)
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


