subplot of plots generated from a for loop

Hi everyone,
I am trying to creat one subplot that has all my 16 plots that were generated from a for loop. I want my subplot to be a 2x8 grid
This is what I am doing but it doesn't seem to work. Any help is appreciated
X= my_data
nCols=3
subplot (2,8,1)
hold on
for iCol=1:nCols:size(X,2)
figure
plot(X(:,iCol:iCol+nCols-1))
end
hold off

 채택된 답변

MJFcoNaN
MJFcoNaN 2022년 4월 9일

0 개 추천

If you need a subplot in for loop, maybe this is an example:
X= my_data
nCols=3
for iCol=1:nCols:size(X,2)
ax=subplot(2, 8, iCol);
plot(ax, X(:,iCol:iCol+nCols-1))
end

댓글 수: 3

Nany
Nany 2022년 4월 9일
I get this error. The argument iCol apparently exceeds the number of subplots (16). I am thinking maybe because it is in a for loop? Also the sub plot produced is 2*6. Is there a way to store the plots produced by the for loop for example, then combining them in a subplot?
% Error using subplot
Index exceeds number of subplots.
Error in duntitled2 (line 13)
aX=subplot(2, 8, iCol )
The object of axes is very flexible for recalling, for example:
for ii=1:16
ax(ii)=subplot(2, 8, ii);
end
plot(ax(3), x3, y3)
But your task may not be such complex, and this could be enough:
count=0;
for iCol=1:nCols:size(X,2)
count=count+1;
ax=subplot(2, 8, count);
plot(ax, X(:,iCol:iCol+nCols-1))
end
Nany
Nany 2022년 4월 9일
Thank you so much! That solved the problem. I really appreciate your help

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

추가 답변 (0개)

카테고리

질문:

2022년 4월 9일

댓글:

2022년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by