필터 지우기
필터 지우기

How to code a figure having subplots with a for loop?

조회 수: 120 (최근 30일)
Hsinho Huang
Hsinho Huang 2017년 1월 7일
편집: balandong 2020년 4월 10일
I want to simply the following codes to make a figure with 2-by-6 subplots. How can I make it with a for loop?
figure(1);
h1=subplot(2,6,1), hLine1=plot(X,A(1,:),X,B(1,:));title(type{1});
h2=subplot(2,6,2), hLine2=plot(X,A(2,:),X,B(2,:));title(type{2});
h3=subplot(2,6,3), hLine3=plot(X,A(3,:),X,B(3,:));title(type{3});
h4=subplot(2,6,4), hLine4=plot(X,A(4,:),X,B(4,:));title(type{4});
h5=subplot(2,6,5), hLine5=plot(X,A(5,:),X,B(5,:));title(type{5});
h6=subplot(2,6,6), hLine6=plot(X,A(6,:),X,B(6,:));title(type{6});
h7=subplot(2,6,7), hLine7=plot(X,A(7,:),X,B(7,:));title(type{7});
h8=subplot(2,6,8), hLine8=plot(X,A(8,:),X,B(8,:));title(type{8});
h9=subplot(2,6,9), hLine9=plot(X,A(9,:),X,B(9,:));title(type{9});
h10=subplot(2,6,10), hLine10=plot(X,A(10,:),X,B(10,:));title(type{10});
h11=subplot(2,6,11), hLine11=plot(X,A(11,:),X,B(11,:));title(type{11});
h12=subplot(2,6,12), hLine12=plot(X,A(12,:),X,B(12,:));title(type{12});
  댓글 수: 1
Stephen23
Stephen23 2017년 1월 8일
편집: Stephen23 2017년 1월 8일
Creating handles h1, h2, h3, etc would be a very bad idea. Read this to know why:
The best solution is to use cell arrays, exactly as Star Strider has shown you.

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

채택된 답변

Star Strider
Star Strider 2017년 1월 7일
I would do it this way:
figure(1)
for k1 = 1:12
subplot(2,6,k1)
h{k1} = plot(X,A(k1,:),X,B(k1,:));title(type{k1});
end
NOTE I do not have your data so this is UNTESTED CODE. It should work.
  댓글 수: 2
Hsinho Huang
Hsinho Huang 2017년 1월 8일
Thanks! In the suggested code, are the handles of each subplot and each line assigned? I need the handles h1-12 and hLine1-12 to set their properties.
Star Strider
Star Strider 2017년 1월 8일
My pleasure!
I didn’t initially see that you had two sets of handles. The revised code would be:
figure(1)
for k1 = 1:12
h{k1} = subplot(2,6,k1)
hLine{k1} = plot(X,A(k1,:),X,B(k1,:));title(type{k1});
end
The handles to the subplots are in the cell array ‘h’, and to the plot in the cell array ‘hLine’.
See the documentation on Cell Arrays if you are not familiar with them.

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

추가 답변 (1개)

balandong
balandong 2020년 4월 6일
I would propose something like @strider, but using curly instead.
figure(1)
for k1 = 1:12
h(k1) = subplot(2,6,k1)
hLine(k1)= plot(X,A(k1,:),X,B(k1,:));title(type{k1});
end
The reason is for, in some circumstances, one would like to link axes among the subplot. And by using the curly braces,, the link axes can be simply achieved as follow
linkaxes(h,'x');
  댓글 수: 2
Priyank Sharma
Priyank Sharma 2020년 4월 10일
However, trying your logic, it shows me subplot (2,1,1), while subplot (2,1,2) on different figures and not on the same figure. I want it should retain (2,1,1) and plot (2,1,2) on the same figure during execution in the loop. Kindly pls suggest.
balandong
balandong 2020년 4월 10일
편집: balandong 2020년 4월 10일
If you are using AppDesigner:
It should be something like below
h(k1) = subplot(2,6,k1,,'Parent',Wathevertheuifigurename) % add the 'Parent'
if you are using normal figure. I would suggest you refer to this FEX submission.

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by