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

84 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 20 Sep 2021
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:

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2021
%% 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');

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!