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

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.

3 Comments

How you have the variables y1,y2...yn in your work space? A matrix? A matfile? A text file?
Data loaded into workspace. Variables assigned by columns; Eg column 1 - t, column 2 - y1 and so on for vertical data.
KSSV, I have the data in excel worksheets. After that some manipulations, I use matlab code to load the excel data into matlab workspace, and another code to plot graphs based on the loaded data.

Sign in to comment.

 Accepted 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.

6 Comments

Thanks. How should I write the code if in the same workspace I have by columns: t1 y1 t2 y2 t3 y3 ... t10 y10
Thanks.
I assume that t is the same for all datasets, as you mentioned in your question. Then,
Y=[y1 y2 y3 y4 y5 y6 y7 y8 y9 y10];
plot(t,Y)
Ok I see now. Forgot to mention that t also change with y. That's why I have t1 ... t10. So what I understand is the code should look like this:
Figure; hold; T=[t1 t2 t3 t4 t5 t6 t7 t8 t9 t10]; Y=[y1 y2 y3 y4 y5 y6 y7 y8 y9 y10]; plot(T,Y)
I am doing interpolation to generate more datasets (so, more graphs). If I have t1...t50 and y1...y50, is there any other ways to plot instead of using array, how to use for loop for example?
Thanks.
For loop will take much more time at every step for larger datasets.
If you create the variables by writing y1, y2,..., not storing them in multidimensional arrays, it will take much more time.
To use for loop, you need to use array from the beginning. Otherwise, by this convention, you will need to do it manually.
Thanks Birdman. Is so, in order to plot 50 graphs for example, its better to make in into array rather than using for loop; array is faster than for loop?
Actually, you do not call it array, call it vectorizing. In either way, you need to store them in array, but vectorizing is much more faster than for loop. Of course there are some points that usage of for loop is inevitable but generally vectorizing is much more efficient. In your case, store each vector in a row of a multidimensional array by using for loop and plot them.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 26 Dec 2017

Commented:

on 27 Dec 2017

Community Treasure Hunt

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

Start Hunting!