Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problems with subplot loop

Subject: Problems with subplot loop

From: Peter Mitrovic

Date: 19 Oct, 2010 21:04:03

Message: 1 of 5

I'm having big problems to get the following subplot loop working. For some reasons after some time in Matlab no error is shown anymore, but most of the time the error is shown:

??? Index exceeds matrix dimensions.

Error in ==> filename at 21
xlabel('Time')

I appologize if this is trivial but I am an absolute beginner and completely stuck.

Code:
m1=0;
m2=[1:241]';
tim=[m1;m2]';
mnret=mondat(:,14:19); %Submatrix for Net Returns
[a b]= size(mnret);
M1=ones(a,b);
mgret=M1+mnret; %Gross Returns
k=ones(1,b);
trind=zeros((a+1),b);
trind(1,:)=ones(1,b);
for j=1:b;
    for i=1:a;
        k(1,j)=k(1,j)*mgret(i,j);
        trind(i+1,j)=k(1,j);
    end;
end;
[a b]= size(trind);
for i=1:6
subplot(3,2,i);
plot(tim,trind(:,i),'b');
xlabel('Time');
ylabel('Total Return');
    if i==1;
title('Total Return Index Microsoft');
        else if i==2;
title('Total Return Index Exxon');
         else if i==3;
title('Total Return Index General Electric');
             else if i==4;
title('Total Return Index JP Morgan');
                   else if i==5;
title('Total Return Index Intel');
                     else if i==6;
title('Total Return Index Citi');
                    end;
                end;
            end;
           end;
        end;
    end;
end;
%loop to create Total Return at every date for all the companies

Subject: Problems with subplot loop

From: Sean

Date: 19 Oct, 2010 21:15:05

Message: 2 of 5

"Peter Mitrovic" <mmitrovic@bluewin.ch> wrote in message <i9l143$3eb$1@fred.mathworks.com>...
> I'm having big problems to get the following subplot loop working. For some reasons after some time in Matlab no error is shown anymore, but most of the time the error is shown:
>
> ??? Index exceeds matrix dimensions.
>
> Error in ==> filename at 21
> xlabel('Time')
>
> I appologize if this is trivial but I am an absolute beginner and completely stuck.
>
> Code:
> m1=0;
> m2=[1:241]';
> tim=[m1;m2]';
> mnret=mondat(:,14:19); %Submatrix for Net Returns
> [a b]= size(mnret);
> M1=ones(a,b);
> mgret=M1+mnret; %Gross Returns
> k=ones(1,b);
> trind=zeros((a+1),b);
> trind(1,:)=ones(1,b);
> for j=1:b;
> for i=1:a;
> k(1,j)=k(1,j)*mgret(i,j);
> trind(i+1,j)=k(1,j);
> end;
> end;
> [a b]= size(trind);
> for i=1:6
> subplot(3,2,i);
> plot(tim,trind(:,i),'b');
> xlabel('Time');
> ylabel('Total Return');
> if i==1;
> title('Total Return Index Microsoft');
> else if i==2;
> title('Total Return Index Exxon');
> else if i==3;
> title('Total Return Index General Electric');
> else if i==4;
> title('Total Return Index JP Morgan');
> else if i==5;
> title('Total Return Index Intel');
> else if i==6;
> title('Total Return Index Citi');
> end;
> end;
> end;
> end;
> end;
> end;
> end;
> %loop to create Total Return at every date for all the companies

Type:
dbstop if error
at the command line. Then run your function and inspect the variables. You're trying to pull data from a matrix outside of that matrix. E.g. if A is size 5x5, you're trying to pull A(7,7) it's going to throw that error.

Subject: Problems with subplot loop

From: Peter Mitrovic

Date: 19 Oct, 2010 21:47:04

Message: 3 of 5

The problem seems to be related to xlabel and ylabel because when I delete them everything works just fine.
I don't really understand though what I did wrong with those labels and the examples in my matllab guide don't look different.

Subject: Problems with subplot loop

From: Sean

Date: 19 Oct, 2010 21:55:07

Message: 4 of 5

"Peter Mitrovic" <mmitrovic@bluewin.ch> wrote in message <i9l3ko$b9t$1@fred.mathworks.com>...
> The problem seems to be related to xlabel and ylabel because when I delete them everything works just fine.
> I don't really understand though what I did wrong with those labels and the examples in my matllab guide don't look different.

More than likely you defined xlabel and ylabel as variables which overwrote the stock functions.
Type:
whos xlabel ylabel

and
which xlabel
which ylabel

and see what happens. If they are indeed variables:
clear xlabel ylabel

Subject: Problems with subplot loop

From: Peter Mitrovic

Date: 19 Oct, 2010 23:05:05

Message: 5 of 5

"Peter Mitrovic" <mmitrovic@bluewin.ch> wrote in message <i9l143$3eb$1@fred.mathworks.com>...
> I'm having big problems to get the following subplot loop working. For some reasons after some time in Matlab no error is shown anymore, but most of the time the error is shown:
>
> ??? Index exceeds matrix dimensions.
>
> Error in ==> filename at 21
> xlabel('Time')
>
> I appologize if this is trivial but I am an absolute beginner and completely stuck.
>
> Code:
> m1=0;
> m2=[1:241]';
> tim=[m1;m2]';
> mnret=mondat(:,14:19); %Submatrix for Net Returns
> [a b]= size(mnret);
> M1=ones(a,b);
> mgret=M1+mnret; %Gross Returns
> k=ones(1,b);
> trind=zeros((a+1),b);
> trind(1,:)=ones(1,b);
> for j=1:b;
> for i=1:a;
> k(1,j)=k(1,j)*mgret(i,j);
> trind(i+1,j)=k(1,j);
> end;
> end;
> [a b]= size(trind);
> for i=1:6
> subplot(3,2,i);
> plot(tim,trind(:,i),'b');
> xlabel('Time');
> ylabel('Total Return');
> if i==1;
> title('Total Return Index Microsoft');
> else if i==2;
> title('Total Return Index Exxon');
> else if i==3;
> title('Total Return Index General Electric');
> else if i==4;
> title('Total Return Index JP Morgan');
> else if i==5;
> title('Total Return Index Intel');
> else if i==6;
> title('Total Return Index Citi');
> end;
> end;
> end;
> end;
> end;
> end;
> end;
> %loop to create Total Return at every date for all the companies

Thanks a lot this was indeed the case, now everything works as intended.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us