필터 지우기
필터 지우기

How to name multiple plots/subplots on a loop?

조회 수: 11 (최근 30일)
Andrew
Andrew 2013년 4월 1일
댓글: Image Analyst 2020년 12월 22일
This loop plots 1 plot with 3 subplots on the same Figure ii times. I cannot figure out how though to label each of the 4 plot with a unique name as the loop progresses. I can use the title, but it only names one of the plots which leaves the other 3 in question to which they are.
for iv = mod((1:numel(k)),12)
k_i = k(iv);
figure (k_i)
DataAnalPlot([c(1:ii,k_i) p(1:ii,k_i)],[c(1:ii,k_i+1) p(1:ii,k_i+1)],[c(1:ii,k_i+2) p(1:ii,k_i+2)],[c(1:ii,k_i+3) p(1:ii,k_i+3)]);
end

채택된 답변

Image Analyst
Image Analyst 2013년 4월 1일
편집: Image Analyst 2013년 4월 1일
First of all, don't use figure() in the loop - use subplot() instead. In the loop you can use sprintf() to build a caption, and then use title() to display it over the plot.
Assuming iv takes on the values 1, 2, 3, and 4:
subplot(2, 2, iv);
caption = sprintf('This is plot #%d', iv);
title(caption, 'FontSize', 20);

추가 답변 (1개)

Nicolas Braconi
Nicolas Braconi 2020년 11월 30일
For any future visitors I figured out that you can do it this way: Create a cell with the title you want each plot to take, for example months, Note that the number of entries in your cell should match the number of subplots, 1 title for each plot
cellmonth={'January','February','March','April','May','June','July'...
,'August','September','October','November','December'};
then you write a for loop with the number of subplots you want
for i=1:12
title(cellmonth(1,j))
.
.
.
.
end
  댓글 수: 3
Rt Ro
Rt Ro 2020년 12월 21일
Hi, thanks for the answer. could you please let me know how I can move each title to up on the left?
Image Analyst
Image Analyst 2020년 12월 22일
Try this:
for k = 1 : 12
subplot(4, 3, k);
caption = sprintf('Month #%d', k); % Caption changes depending on k
title(caption, 'FontSize', 20, 'HorizontalAlignment', 'right');
end
I have no idea why you say 'right' when you want to put it on the left. Seems like a bug.

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

카테고리

Help CenterFile Exchange에서 Formatting and Annotation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by