Plotting multiple figures from a for loop on the same graph

조회 수: 64 (최근 30일)
R
R 2014년 5월 7일
댓글: Arjun Upadhyay 2020년 8월 12일
Greetings. I am an introductory level matlab user and fairly inexperienced and writing code so please bear with me. I was handed some code and told to plot the outputs (two separate outputs) from the loop function onto a set of graphs. The loop runs, but only outputs the last file's data to the two graphs. I have included my code in the link below as it is somewhat long.
If I run the loop for the entirety of my code (not stopping at line 33) then I get the output from each file on a separate graph (30 graphs!) so I'm certain that the loop is not the issue.
Edit:
I figured out what my problem was. I needed to add individual loops for each figure.

답변 (1개)

Justin
Justin 2014년 5월 7일
편집: Justin 2014년 5월 7일
I'm not sure I understand exactly what you are trying to get but I will try to help.
Matlab nomenclature usually refers to the window as a "figure" while the graph is the boxed area that the data is displayed. You can have one figure window with multiple graphs (or plots or axes) on it. With 30 different graphs the window would get pretty crowded though.
If you want one figure with set of axes and all the data on that same axes you can use
hold on
command and the data won't erase when you plot something else. Make sure to use
hold off
after the loop to disable the behavior.
This is used some on lines 45 and 54. Also the first figure is created using
fig = figure
and is recreated and overwritten each time in the loop. If you want all the data in the same figure you can create the figure once before the loop starts and use the value in the variable fig as a handle reference.
Also on line 44 the figure is again calling the fig handle to plot.
Somethings to look at that may be useful are subplots and handles.
Let me know if this helps and if you have any further questions.
EDIT:
Try this as an example to understand the plotting behavior.
dat = rand([10 20]);
figure(); hold on
for i = 1:20
plot(dat(:, i));
end
hold off
  댓글 수: 2
Sophia Dominguez
Sophia Dominguez 2015년 1월 28일
What if you want to plot the random data on 20 different plots and have those as 20 separate figures?
Arjun Upadhyay
Arjun Upadhyay 2020년 8월 12일
Can we assign the value for each plot or color coded them ?

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by