Plotting multiple figures from a for loop on the same graph

59 views (last 30 days)
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.

Answers (1)

Justin
Justin on 7 May 2014
Edited: Justin on 7 May 2014
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 Comments
Sophia Dominguez
Sophia Dominguez on 28 Jan 2015
What if you want to plot the random data on 20 different plots and have those as 20 separate figures?
Arjun Upadhyay
Arjun Upadhyay on 12 Aug 2020
Can we assign the value for each plot or color coded them ?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!