Thread Subject: A Simple Question

Subject: A Simple Question

From: Virtualized

Date: 21 Nov, 2009 21:12:06

Message: 1 of 10

Hi Everybody

I have the following simple script:

if P(1) == 0
         return;
     else
         P1=P(1)

     if P(2) == 0
         return;
     else
         P2=P(2)

         if P(3) == 0
         return;
     else
         P3=P(3)
     end
         end
     end

If i don't have any value at any location of the matrix or if the
value is "0" then i want the program to stop executing.

Suppose i have

P=[1,2 ];

Here i have only two values for P(1) and P(2) so when i execute script
then i get the following error:

??? Attempted to access P(3); index out of bounds because numel(P)=2.

Error in ==> p at 13
         if P(3) == 0

Any help in this regard is highly appreciated.

Regards

Subject: A Simple Question

From: MatlabFCT NG

Date: 21 Nov, 2009 21:43:56

Message: 2 of 10

Could you please structure better your script?

On 21 Nov, 21:12, Virtualized <razzaqa...@gmail.com> wrote:
> Hi Everybody
>
> I have the following simple script:
>
> if P(1) == 0
>          return;
>      else
>          P1=P(1)
>
>      if P(2) == 0
>          return;
>      else
>          P2=P(2)
>
>          if P(3) == 0
>          return;
>      else
>          P3=P(3)
>      end
>          end
>      end
>
> If i don't have any value at any location of the matrix or if the
> value is "0"  then i want the program to stop executing.
>
> Suppose i have
>
> P=[1,2 ];
>
> Here i have only two values for P(1) and P(2) so when i execute script
> then i get the following error:
>
> ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> Error in ==> p at 13
>          if P(3) == 0
>
> Any help in this regard is highly appreciated.
>
> Regards

Subject: A Simple Question

From: Virtualized

Date: 21 Nov, 2009 22:11:04

Message: 3 of 10

On Nov 21, 10:43 pm, MatlabFCT NG <matfc...@gmail.com> wrote:
> Could you please structure better your script?
>
> On 21 Nov, 21:12, Virtualized <razzaqa...@gmail.com> wrote:
>
>
>
> > Hi Everybody
>
> > I have the following simple script:
>
> > if P(1) == 0
> >          return;
> >      else
> >          P1=P(1)
>
> >      if P(2) == 0
> >          return;
> >      else
> >          P2=P(2)
>
> >          if P(3) == 0
> >          return;
> >      else
> >          P3=P(3)
> >      end
> >          end
> >      end
>
> > If i don't have any value at any location of the matrix or if the
> > value is "0"  then i want the program to stop executing.
>
> > Suppose i have
>
> > P=[1,2 ];
>
> > Here i have only two values for P(1) and P(2) so when i execute script
> > then i get the following error:
>
> > ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> > Error in ==> p at 13
> >          if P(3) == 0
>
> > Any help in this regard is highly appreciated.
>
> > Regards

Hi

I am writing it again. The script is:

if P(1) == 0
        return;
    else
        P1=P(1)

    if P(2) == 0
        return;
    else
        P2=P(2)

        if P(3) == 0
        return;
    else
        P3=P(3)
    end
        end
    end

Subject: A Simple Question

From: Virtualized

Date: 21 Nov, 2009 22:15:14

Message: 4 of 10

On Nov 21, 10:43 pm, MatlabFCT NG <matfc...@gmail.com> wrote:
> Could you please structure better your script?
>
> On 21 Nov, 21:12, Virtualized <razzaqa...@gmail.com> wrote:
>
>
>
> > Hi Everybody
>
> > I have the following simple script:
>
> > if P(1) == 0
> >          return;
> >      else
> >          P1=P(1)
>
> >      if P(2) == 0
> >          return;
> >      else
> >          P2=P(2)
>
> >          if P(3) == 0
> >          return;
> >      else
> >          P3=P(3)
> >      end
> >          end
> >      end
>
> > If i don't have any value at any location of the matrix or if the
> > value is "0"  then i want the program to stop executing.
>
> > Suppose i have
>
> > P=[1,2 ];
>
> > Here i have only two values for P(1) and P(2) so when i execute script
> > then i get the following error:
>
> > ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> > Error in ==> p at 13
> >          if P(3) == 0
>
> > Any help in this regard is highly appreciated.
>
> > Regards

Sorry for the previous post. I am going to rewrite it again:

  if P(1) == 0
        return;
    else
        P1=P(1);

      if P(2) == 0
        return;
      else
         P2=P(2)

           if P(3) == 0
           return;
          else
         P3=P(3)
        end

       end

end


When i give it the following vector as input:

P=[1,2 ];

I get the following error:

??? Attempted to access P(3); index out of bounds because numel(P)=2.

Error in ==> p at 13
        if P(3) == 0


Thank you for your response.

Regards

Subject: A Simple Question

From: Pedro Manuel

Date: 21 Nov, 2009 22:46:02

Message: 5 of 10

Hi Virtualized,

Here is your code:

P=[1,2];

if P(1) == 0
    return;
else
    P1=P(1);

    if P(2) == 0
        return;
    else
        P2=P(2);

        if P(3) == 0 %problem in this line
            return;
        else
            P3=P(3);
        end
    end
end

Your problem is in the line 13, there you are asking a question to computer: is P(3) equal to 0? However you don't have in your matrix any value in that position, therefore the computer doesn't have any number to compare... and that's why you have the error...

If you change your matrix P to P=[1,2,3], you'll not have any problem...

Virtualized <razzaqadil@gmail.com> wrote in message <346ea9a3-b20a-497b-a4eb-dab001daf391@k4g2000yqb.googlegroups.com>...
> On Nov 21, 10:43?pm, MatlabFCT NG <matfc...@gmail.com> wrote:
> > Could you please structure better your script?
> >
> > On 21 Nov, 21:12, Virtualized <razzaqa...@gmail.com> wrote:
> >
> >
> >
> > > Hi Everybody
> >
> > > I have the following simple script:
> >
> > > if P(1) == 0
> > > ? ? ? ? ?return;
> > > ? ? ?else
> > > ? ? ? ? ?P1=P(1)
> >
> > > ? ? ?if P(2) == 0
> > > ? ? ? ? ?return;
> > > ? ? ?else
> > > ? ? ? ? ?P2=P(2)
> >
> > > ? ? ? ? ?if P(3) == 0
> > > ? ? ? ? ?return;
> > > ? ? ?else
> > > ? ? ? ? ?P3=P(3)
> > > ? ? ?end
> > > ? ? ? ? ?end
> > > ? ? ?end
> >
> > > If i don't have any value at any location of the matrix or if the
> > > value is "0" ?then i want the program to stop executing.
> >
> > > Suppose i have
> >
> > > P=[1,2 ];
> >
> > > Here i have only two values for P(1) and P(2) so when i execute script
> > > then i get the following error:
> >
> > > ??? Attempted to access P(3); index out of bounds because numel(P)=2.
> >
> > > Error in ==> p at 13
> > > ? ? ? ? ?if P(3) == 0
> >
> > > Any help in this regard is highly appreciated.
> >
> > > Regards
>
> Sorry for the previous post. I am going to rewrite it again:
>
> if P(1) == 0
> return;
> else
> P1=P(1);
>
> if P(2) == 0
> return;
> else
> P2=P(2)
>
> if P(3) == 0
> return;
> else
> P3=P(3)
> end
>
> end
>
> end
>
>
> When i give it the following vector as input:
>
> P=[1,2 ];
>
> I get the following error:
>
> ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> Error in ==> p at 13
> if P(3) == 0
>
>
> Thank you for your response.
>
> Regards

Subject: A Simple Question

From: Pedro Manuel

Date: 21 Nov, 2009 22:47:03

Message: 6 of 10

Hi Virtualized,

Here is your code:

P=[1,2];

if P(1) == 0
    return;
else
    P1=P(1);

    if P(2) == 0
        return;
    else
        P2=P(2);

        if P(3) == 0 %problem in this line
            return;
        else
            P3=P(3);
        end
    end
end

Your problem is in the line 13, there you are asking a question to computer: is P(3) equal to 0? However you don't have in your matrix any value in that position, therefore the computer doesn't have any number to compare... and that's why you have the error...

If you change your matrix P to P=[1,2,3], you'll not have any problem...

Virtualized <razzaqadil@gmail.com> wrote in message <346ea9a3-b20a-497b-a4eb-dab001daf391@k4g2000yqb.googlegroups.com>...
> On Nov 21, 10:43?pm, MatlabFCT NG <matfc...@gmail.com> wrote:
> > Could you please structure better your script?
> >
> > On 21 Nov, 21:12, Virtualized <razzaqa...@gmail.com> wrote:
> >
> >
> >
> > > Hi Everybody
> >
> > > I have the following simple script:
> >
> > > if P(1) == 0
> > > ? ? ? ? ?return;
> > > ? ? ?else
> > > ? ? ? ? ?P1=P(1)
> >
> > > ? ? ?if P(2) == 0
> > > ? ? ? ? ?return;
> > > ? ? ?else
> > > ? ? ? ? ?P2=P(2)
> >
> > > ? ? ? ? ?if P(3) == 0
> > > ? ? ? ? ?return;
> > > ? ? ?else
> > > ? ? ? ? ?P3=P(3)
> > > ? ? ?end
> > > ? ? ? ? ?end
> > > ? ? ?end
> >
> > > If i don't have any value at any location of the matrix or if the
> > > value is "0" ?then i want the program to stop executing.
> >
> > > Suppose i have
> >
> > > P=[1,2 ];
> >
> > > Here i have only two values for P(1) and P(2) so when i execute script
> > > then i get the following error:
> >
> > > ??? Attempted to access P(3); index out of bounds because numel(P)=2.
> >
> > > Error in ==> p at 13
> > > ? ? ? ? ?if P(3) == 0
> >
> > > Any help in this regard is highly appreciated.
> >
> > > Regards
>
> Sorry for the previous post. I am going to rewrite it again:
>
> if P(1) == 0
> return;
> else
> P1=P(1);
>
> if P(2) == 0
> return;
> else
> P2=P(2)
>
> if P(3) == 0
> return;
> else
> P3=P(3)
> end
>
> end
>
> end
>
>
> When i give it the following vector as input:
>
> P=[1,2 ];
>
> I get the following error:
>
> ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> Error in ==> p at 13
> if P(3) == 0
>
>
> Thank you for your response.
>
> Regards

Subject: A Simple Question

From: Virtualized

Date: 21 Nov, 2009 23:05:30

Message: 7 of 10

On Nov 21, 11:47 pm, "Pedro Manuel " <matfc...@gmail.com> wrote:
> Hi Virtualized,
>
> Here is your code:
>
> P=[1,2];
>
> if P(1) == 0
>     return;
> else
>     P1=P(1);
>
>     if P(2) == 0
>         return;
>     else
>         P2=P(2);
>
>         if P(3) == 0 %problem in this line
>             return;
>         else
>             P3=P(3);
>         end
>     end
> end
>
> Your problem is in the line 13, there you are asking a question to computer: is P(3) equal to 0? However you don't have in your matrix any value in that position, therefore the computer doesn't have any number to compare... and that's why you have the error...
>
> If you change your matrix P to P=[1,2,3], you'll not have any problem...
>
>
>
> Virtualized <razzaqa...@gmail.com> wrote in message <346ea9a3-b20a-497b-a4eb-dab001daf...@k4g2000yqb.googlegroups.com>...
> > On Nov 21, 10:43?pm, MatlabFCT NG <matfc...@gmail.com> wrote:
> > > Could you please structure better your script?
>
> > > On 21 Nov, 21:12, Virtualized <razzaqa...@gmail.com> wrote:
>
> > > > Hi Everybody
>
> > > > I have the following simple script:
>
> > > > if P(1) == 0
> > > > ? ? ? ? ?return;
> > > > ? ? ?else
> > > > ? ? ? ? ?P1=P(1)
>
> > > > ? ? ?if P(2) == 0
> > > > ? ? ? ? ?return;
> > > > ? ? ?else
> > > > ? ? ? ? ?P2=P(2)
>
> > > > ? ? ? ? ?if P(3) == 0
> > > > ? ? ? ? ?return;
> > > > ? ? ?else
> > > > ? ? ? ? ?P3=P(3)
> > > > ? ? ?end
> > > > ? ? ? ? ?end
> > > > ? ? ?end
>
> > > > If i don't have any value at any location of the matrix or if the
> > > > value is "0" ?then i want the program to stop executing.
>
> > > > Suppose i have
>
> > > > P=[1,2 ];
>
> > > > Here i have only two values for P(1) and P(2) so when i execute script
> > > > then i get the following error:
>
> > > > ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> > > > Error in ==> p at 13
> > > > ? ? ? ? ?if P(3) == 0
>
> > > > Any help in this regard is highly appreciated.
>
> > > > Regards
>
> > Sorry for the previous post. I am going to rewrite it again:
>
> >   if P(1) == 0
> >         return;
> >     else
> >         P1=P(1);
>
> >       if P(2) == 0
> >         return;
> >       else
> >          P2=P(2)
>
> >            if P(3) == 0
> >            return;
> >           else
> >          P3=P(3)
> >         end
>
> >        end
>
> > end
>
> > When i give it the following vector as input:
>
> > P=[1,2 ];
>
> > I get the following error:
>
> > ??? Attempted to access P(3); index out of bounds because numel(P)=2.
>
> > Error in ==> p at 13
> >         if P(3) == 0
>
> > Thank you for your response.
>
> > Regards

Hi

Yes i know that. But as i stated in my first message, i want to check
both conditions if
(1) either P(3) or P(2) or P(1) == 0 or
(2) there is no value at any location

then program should execute the "return" statement.

Thank you once again for your help.

Regards

Subject: A Simple Question

From: Pedro Manuel

Date: 21 Nov, 2009 23:40:23

Message: 8 of 10

Hi again :),

Even if you don't have any matrix P, you want to make your program return... Is that it?
Maybe you can use try catch, is not a fancy way to do it. I'm sorry but i don't remember other now.
**********************
try
     % your code
catch
      return
end
**********************

Subject: A Simple Question

From: Jan Simon

Date: 22 Nov, 2009 11:55:03

Message: 9 of 10

Dear Virtualized!

> Yes i know that. But as i stated in my first message, i want to check
> both conditions if
> (1) either P(3) or P(2) or P(1) == 0 or
> (2) there is no value at any location

Do you mean:
  if ~all(P) || length(P) < 3
    return;
  end

Kind regards, Jan

Subject: A Simple Question

From: Virtualized

Date: 22 Nov, 2009 12:21:13

Message: 10 of 10

On Nov 22, 12:55 pm, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de>
wrote:
> Dear Virtualized!
>
> > Yes i know that. But as i stated in my first message, i want to check
> > both conditions if
> > (1) either P(3) or P(2) or P(1) == 0 or
> > (2) there is no value at any location
>
> Do you mean:
>   if ~all(P) || length(P) < 3
>     return;
>   end
>
> Kind regards, Jan

Dear Jan

Thanks a lot.

Yes that is exactly what i wanted to achieve.

Best Regards

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com