0
votes

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!

2
When you use 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

2 Answers

0
votes

The third argument in subplot can't exceed the total number of subplots. For example, when you use subplot(5, 4, ...), it means that there will be a total of 5*4 = 20 subplots, so the third argument can't be 21.

So you need to create a new figure using the figure command, and then create the next 20 subplots. In the end, you will have 5 different figures.

So your code will look something like this:

figure(1);    % First figure window
for ii = 1:20
    subplot(5,4,ii)    % create first 20 subplots
    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
figure(2);    % second figure window
for ii = 21:40
    subplot(5,4,ii-20)    % create next 20 subplots
    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
...
0
votes

You get the error because subplot expects to get 5x4 = 20 subplots, whereas you start the loop telling it to position the plot in the 21st spot, which does not work.

You can simply subtract 20 from the index in the call to subplot to make sure yo are in the right range.

Eg.

subplot(5,4,ii-20)

yielding:

enter image description here