Fail to plot graphs in a for loop, hold on doesn't work

6 views (last 30 days)
Hi all,
My codes intend to plot a graph from two tables using a for loop, but the "hold on" function does not work. No matter where I put the "hold on" (i.e. inside or outside the loop), only the last plot is shown in the figure. I am using Matlab2016a
hold on
for filei=0: ((length(vel_faster.Properties.VariableNames))-4)/3
column_y=3*filei+2; % y coordinates
column_yL=3*filei+3; % y coordinates
column_yU=3*filei+4; % y coordinates
column_b=3*filei+2; % y coordinates
column_bL=3*filei+3; % y coordinates
column_bU=3*filei+4; % y coordinates
figurei=filei+1; %file loop
% Plot lines from first table
hold on;
x=vel_slower.(1);
y=vel_slower.(column_y); %line1
yL=vel_slower.(column_yL);%line2
yU=vel_slower.(column_yU);%line3
figure(figurei); plot(x,y,'k-');
figure(figurei); plot(x,yU,'m--');
figure(figurei); plot(x,yL,'m--');
% Plot lines from second table
a=vel_faster.(1);
b=vel_faster.(column_b); %line4
bL=vel_faster.(column_bL); %line5
bU=vel_faster.(column_bU); %line6
figure(figurei); plot(a,b,'r-');
figure(figurei); plot(a,bL,'g:');
figure(figurei); plot(a,bU,'g:');
hold all;
end

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 4 Jan 2017
Put the "hold on" command after the figure() line.
"hold" holds the current plot.
"figure(H)" makes H the current figure. If Figure H does not exist, and H is an integer, a new figure is created with handle H.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!