How to plot results from each iteration of a for loop SEPARATELY

39 views (last 30 days)
Hi all,
I would like to know how i can plot results obtained through a for loop on seperate plots. I understand that i can plot each itteration result on the same plot, but i need to plot each itteration on a seperate plot so they can be closely analysed.
Any help would be great, thanks !

Accepted Answer

Dave B
Dave B on 16 Nov 2021
You can do this in separate windows using the figure function:
for i = 1:3
figure
plot(rand(1,10))
end
In separate axes within a figure using nexttile (requires 2019b or later, for earlier versions see subplot)
figure;
tiledlayout('flow');
for i = 1:10
nexttile
plot(rand(1,10))
end
(or if you want them in a specific grid shape):
figure
t=tiledlayout(5,2);
for i = 1:10
nexttile
plot(rand(1,10))
end
Or you can try stackedplot if you don't have too many and you can put them all into a table or matrix:
figure
stackedplot(rand(100,4))
  2 Comments
Joshua Pretorius
Joshua Pretorius on 16 Nov 2021
Thanks Dave, works perfectly !
is there a way to generate different titles for each plot generated, for the first method you suggested ? so it would generate titles like Figure1, Figure 2 etc for each plot
Dave B
Dave B on 16 Nov 2021
yep, you can use the title command with any of these methods. There are surprsingly many approaches to appending a number onto a word (Figure 1), but the easiest in my opinion is to use strings like this:
for i = 1:3
figure
plot(rand(1,10))
title("Figure " + i)
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!