필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Mohd Sapiee
Mohd Sapiee 2017년 12월 26일
댓글: Birdman 2017년 12월 27일
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
Mohd Sapiee
Mohd Sapiee 2017년 12월 26일
Data loaded into workspace. Variables assigned by columns; Eg column 1 - t, column 2 - y1 and so on for vertical data.
Mohd Sapiee
Mohd Sapiee 2017년 12월 27일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Birdman
Birdman 2017년 12월 26일
편집: Birdman 2017년 12월 26일
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
Mohd Sapiee
Mohd Sapiee 2017년 12월 27일
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?
Birdman
Birdman 2017년 12월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by