MATLAB: How to combine multiple plots in a for loop

for loopMATLABplots

Hello all,
I am trying to plot within a for loop, but it only plots the last value, as it's overwritting previous values. My code look like the following:
BLOCK = 5x1 cell array
TIME = 5x1 cell array
for i=length(BLOCK)
plot(TIME(i),BLOCK(i))
end
Not sure how to get this, so it doesn't overwrite previous values. Thank you

Best Answer

Add “hold on” prior to your for-loop.
Adding a "hold on" command means that anything that you plot will not clear the existing graph, but just plot on top of what is already there. You can turn off this functionality with the "hold off" command.
for k = 1:length(BLOCK)
plot(TIME(k),BLOCK(k))
if k == 1
hold on
end
end
hold off