Hi all,
I can't seem to find a solution for this simple problem:
I have a for loop with an output of 8 plots. I would like to group them as they come out in 2 subplots of 4. How can I do this?
KR,
KMT.

 채택된 답변

Cedric
Cedric 2017년 10월 2일
편집: Cedric 2017년 10월 2일

7 개 추천

figure() ;
for plotId = 1 : 4
subplot(2, 2, plotId) ;
plot(x{plotId}, y{plotId}) ;
end
figure() ;
for plotId = 1 : 4
subplot(2, 2, plotId) ;
plot(x{plotId+4}, y{plotId+4}) ;
end
or in one loop, but it adds some complexity that may not be that useful:
for plotId = 1 : 8
if ~mod(plotId-1, 4)
figure() ;
end
subplot(2, 2, mod(plotId-1, 4)+1) ;
plot(x{plotId}, y{plotId}) ;
end
Finally, if you wanted to define your own axes to avoid all the space left by SUBPLOT and fit your 8 plots in one figure, you could do it as illustrated in my answer here:

댓글 수: 4

Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 10월 2일
Thanks for your answer!
Is there a way to do it without splitting it into two loops? I am asking cause I get my results form a for loop so it would be very convenient to run one subplot command if possible within that for loop.
Cedric
Cedric 2017년 10월 2일
편집: Cedric 2017년 10월 2일
See my updated answer. Sorry, I apparently updated it while you were reading the first version.
You can see how that works by evaluating a few expressions:
>> mod((1:8)-1, 4)
ans =
0 1 2 3 0 1 2 3
>> ~mod((1:8)-1, 4) % For triggering the creation of a new figure.
ans =
1×8 logical array
1 0 0 0 1 0 0 0
>> mod((1:8)-1, 4)+1 % For indexing the sub plot.
ans =
1 2 3 4 1 2 3 4
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 10월 2일
Very elegant! Thank you for your answer, works very well
Cedric
Cedric 2017년 10월 3일
My pleasure!

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

추가 답변 (0개)

카테고리

질문:

2017년 10월 2일

댓글:

2017년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by