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

조회 수: 105 (최근 30일)
Joshua Pretorius
Joshua Pretorius 2021년 11월 16일
댓글: Dave B 2021년 11월 16일
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 !

채택된 답변

Dave B
Dave B 2021년 11월 16일
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
Joshua Pretorius
Joshua Pretorius 2021년 11월 16일
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 2021년 11월 16일
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

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by