add multiple lines to a single plot without using "hold on/off"

조회 수: 72 (최근 30일)
cdlapoin
cdlapoin 2021년 9월 20일
댓글: cdlapoin 2021년 9월 20일
I have a function that creates two plots, I want to run that function in a for loop but have the plots updated with the new lines rather than create new plots. If I use hold on, then the two different types of plot will be on the same set of axis so I don't want that.
%% Create Plots
% Would like pressure dist overlaid on airfoil shape
tiledlayout(2,1);
ax1 = nexttile;
plot(x,y);
hold on;
for i=1:numPanels
p = [loc(i,1) loc(i,2); loc(i,1)-0.1*Cp(i)*normVect(i,1) loc(i,2)-0.1*Cp(i)*normVect(i,2)];
plot(p(:,1),p(:,2));
end
hold off
% plot CP dist below with same x-axis
ax2 = nexttile;
plot(loc(:,1),Cp);
linkaxes([ax1 ax2],'x');
set(ax2, 'YDIR','reverse');
so with this example code I would like the first plot to conitunally update ax1 on each itteration and the second to update ax2 for each itteration.
Thank you
  댓글 수: 2
Stephen23
Stephen23 2021년 9월 20일
The solution, just like all robust graphics, is to explicitly obtain and refer to all graphics objects via their handles.
As soon as you move beyond simple plotting one line in one figure, then you need to start working with handles:
cdlapoin
cdlapoin 2021년 9월 20일
Thanks, that is what I was looking for

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 20일
%% Create Plots
% Would like pressure dist overlaid on airfoil shape
tiledlayout(2,1);
ax1 = nexttile;
plot(ax1,x,y);
hold(ax1,'on');
for i=1:numPanels
p = [loc(i,1) loc(i,2); loc(i,1)-0.1*Cp(i)*normVect(i,1) loc(i,2)-0.1*Cp(i)*normVect(i,2)];
plot(ax1, p(:,1),p(:,2));
end
hold(ax1, 'off')
% plot CP dist below with same x-axis
ax2 = nexttile;
plot(ax2, loc(:,1), Cp);
linkaxes([ax1 ax2],'x');
set(ax2, 'YDIR','reverse');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Automated Driving Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by