Plotting multiple figures from a for loop on the same graph

66 次查看(过去 30 天)
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?

请先登录,再进行评论。

类别

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