I am working on a problem in matlab where I am asked to plot an age distribution for 100 years, requiring 100 plots. I am looking to create 5 separate plots with 20 subplots in each for a total of 100. I can't seem to get past the first 20 plots without getting an error in my subplot command.
Here is the initial part of the code:
>> %initial age distribution
x0=[10;10;10];
%projection matrix
M=[0 4 3; 0.6 0 0; 0 0.25 0];
%calculate the total population size and age distribution for the first 100 years
X=zeros(3, 100);
X(:,1) = x0;
for kk = 2:100
X(:,kk) = M*X(:,kk-1);
end
%we need to double the population to account for males, assuming that population is ½ males and ½ females
X=2*X
My initial plan was to create a 10x10 subplot but it looks awful, but this is the code I generated.
for ii = 1:100
subplot(10,10,ii)
set(gca,'FontSize',12)
barh(log10(X(:,ii)));
set(gca,'YTickLabel',{'Y1' 'Y2' 'Y3'})
xlabel('log(popn.)')
title(['Y' ,num2str(ii)])
axis([-1 25 0.5 3.5])
end
So i decided to do it in chunks of 20.
Here's what I have:
for ii = 1:20
subplot(5,4,ii)
set(gca,'FontSize',12)
barh(log10(X(:,ii)));
set(gca,'YTickLabel',{'Y1' 'Y2' 'Y3'})
xlabel('log10(population)')
title(['Y' ,num2str(ii)])
axis([-1 25 0.5 3.5])
end
When I try from 21 to 40, I get an error as follows:
for ii = 21:40
subplot(5,4,ii)
set(gca,'FontSize',12)
barh(log10(X(:,ii)));
set(gca,'YTickLabel',{'Y1' 'Y2' 'Y3'})
xlabel('log10(population)')
title(['Y' ,num2str(ii)])
axis([-1 25 0.5 3.5])
end
Error using subplot (line 280)
Index exceeds number of subplots.
I need to repeat for 21-40, 41-60, 61-80, and 81-100. Any help would be greatly appreciated. I am new to matlab.
Thanks!

subplot(a, b, idx)you have idx available in the range [1 a*b] where a is the number of rows and b is the number of columns within the figure. - DreamBig