MATLAB: How to plot graphs iteratively or using for loop on the same figure

plot multiple graphs

I have datasets that I can use to plot several graphs on the same time scale. For example the time variable is t and the variable for vertical axis is y1, y2, y3, … y10. Manually I can use these codes: figure; hold; plot(t, y1); plot(t, y2); plot(t, y3); … plot(t, y10);
But, how to simplify the process using iterative or for loop? The manual method will be a problem if there are many graphs to plot. We cannot manually type for example plot(t,y1) up to plot(t, y50).
Would appreciate if anyone can provide a simple code based on the example given.
Thank you.

Best Answer

Instead storing your plotting data in y1,y2,..,y50, store them in a multidimensional array where each column will represent each set of data. For instance
y(:,1)=y1
y(:,2)=y2
.
.
If you do this, all you need to do is
plot(t,y)
of course, t has to have same number of row with y array in this case.