Multiple case function error

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple case function error

harish3679
This post was updated on .
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I dont know what is going wrong,Here is the code:

x = {d = 3, e = 4, f = 5};
a = {g = 7, h = 2, i = 6};
b = {k = 8, l = 1, m = 9};

y = input('enter a value: ');
switch(y)

case d
  xpos = {d, h, m};
  xneg = {k, h, f};
 
  cross = input('enter cross value: ');
 
case g
  xxpos = {g, h, d};
  xxneg = {f, i, e};
 
  cross = input('enter cross value: ');
 
endswitch

switch(cross)

case {xpos, xxpos}
  max = d+h;
  disp(max)
 
case {xneg, xxneg}
  min = g-f;
  disp(min)
endswitch

Best,
Harish



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

nrjank
Administrator


On Wed, Apr 1, 2020 at 1:31 PM harish3679 <[hidden email]> wrote:
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I've had some trouble even trying to recreate your error.  

1 - did you define some values for d, etc, outside of what you showed? because if I run your script from a clean workspace i get:

enter a value: d
error: 'd' undefined near line 1 column 1
error: called from
    casetest at line 3 column 3

i notice some other things.  You use cross as a variable name, but it is the name of a function for performing a vector cross product.  so if the first switch doesn't set a value to overwrite the meaning of cross, you get an error calling the switch(cross) line. 

do you mean for your switch(y) block to match the letter input d or g?  in that case you need it to be defined as a character with quotation marks,  'd', or "d".  Of course, then it's not clear what values will be going into xpos or xxpos.

look at the help for switch with:  >> help switch





Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

harish3679
Thank you! I changed cross variable name. Yes, switch y block input should match the output of cross block to obtain either xpos or xneg value. But, when I use 'd' its ignoring the case statement and skipping forward. Can I use If else condition? something similar to switch case statement though 

On Thu, Apr 2, 2020 at 2:14 AM Nicholas Jankowski <[hidden email]> wrote:


On Wed, Apr 1, 2020 at 1:31 PM harish3679 <[hidden email]> wrote:
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I've had some trouble even trying to recreate your error.  

1 - did you define some values for d, etc, outside of what you showed? because if I run your script from a clean workspace i get:

enter a value: d
error: 'd' undefined near line 1 column 1
error: called from
    casetest at line 3 column 3

i notice some other things.  You use cross as a variable name, but it is the name of a function for performing a vector cross product.  so if the first switch doesn't set a value to overwrite the meaning of cross, you get an error calling the switch(cross) line. 

do you mean for your switch(y) block to match the letter input d or g?  in that case you need it to be defined as a character with quotation marks,  'd', or "d".  Of course, then it's not clear what values will be going into xpos or xxpos.

look at the help for switch with:  >> help switch





--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

harish3679
This is what I have done now

x = {d = 3, e = 4, f = 5};
a = {g = 7, h = 2, i = 6};
b = {k = 8, l = 1, m = 9};

d = x(1,1);
e = x(1,2);
f = x(1,3);
g = a(1,1);
h = a(1,2);
i = a(1,3);
k = b(1,1);
i = b(1,2);
m = b(1,3);

y = input('enter a value: ');
switch(y)

case d
  xpos ={d, h, m};
  xneg ={k, h, f};
 
  ss = input('enter cross value: ');
 
case g
  xxpos = {g, h, d};
  xxneg = {f, i, e};
 
  ss = input('enter cross value: ');

endswitch

if (ss = xpos && xxpos)


  max = d+h;
  disp(max)
 
else
 
  min = g-f;
  disp(min)
 
endif

but now, I am getting these errors

enter a value: d
enter cross value: h
error: invalid conversion from cell array to logical value
error: called from
    crossminmax at line 32 column 1

enter a value: d
error: binary operator '==' not implemented for 'cell' by 'scalar' operations
error: called from
    crossminmax at line 16 column 1

On Thu, Apr 2, 2020 at 12:05 PM Harishkumar U <[hidden email]> wrote:
Thank you! I changed cross variable name. Yes, switch y block input should match the output of cross block to obtain either xpos or xneg value. But, when I use 'd' its ignoring the case statement and skipping forward. Can I use If else condition? something similar to switch case statement though 

On Thu, Apr 2, 2020 at 2:14 AM Nicholas Jankowski <[hidden email]> wrote:


On Wed, Apr 1, 2020 at 1:31 PM harish3679 <[hidden email]> wrote:
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I've had some trouble even trying to recreate your error.  

1 - did you define some values for d, etc, outside of what you showed? because if I run your script from a clean workspace i get:

enter a value: d
error: 'd' undefined near line 1 column 1
error: called from
    casetest at line 3 column 3

i notice some other things.  You use cross as a variable name, but it is the name of a function for performing a vector cross product.  so if the first switch doesn't set a value to overwrite the meaning of cross, you get an error calling the switch(cross) line. 

do you mean for your switch(y) block to match the letter input d or g?  in that case you need it to be defined as a character with quotation marks,  'd', or "d".  Of course, then it's not clear what values will be going into xpos or xxpos.

look at the help for switch with:  >> help switch





--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

James Sherman

On Thu, Apr 2, 2020 at 2:40 AM Harishkumar U <[hidden email]> wrote:
This is what I have done now

x = {d = 3, e = 4, f = 5};
a = {g = 7, h = 2, i = 6};
b = {k = 8, l = 1, m = 9};

d = x(1,1);
e = x(1,2);
f = x(1,3);
g = a(1,1);
h = a(1,2);
i = a(1,3);
k = b(1,1);
i = b(1,2);
m = b(1,3);

y = input('enter a value: ');
switch(y)

case d
  xpos ={d, h, m};
  xneg ={k, h, f};
 
  ss = input('enter cross value: ');
 
case g
  xxpos = {g, h, d};
  xxneg = {f, i, e};
 
  ss = input('enter cross value: ');

endswitch

if (ss = xpos && xxpos)


  max = d+h;
  disp(max)
 
else
 
  min = g-f;
  disp(min)
 
endif

but now, I am getting these errors

enter a value: d
enter cross value: h
error: invalid conversion from cell array to logical value
error: called from
    crossminmax at line 32 column 1

enter a value: d
error: binary operator '==' not implemented for 'cell' by 'scalar' operations
error: called from
    crossminmax at line 16 column 1

On Thu, Apr 2, 2020 at 12:05 PM Harishkumar U <[hidden email]> wrote:
Thank you! I changed cross variable name. Yes, switch y block input should match the output of cross block to obtain either xpos or xneg value. But, when I use 'd' its ignoring the case statement and skipping forward. Can I use If else condition? something similar to switch case statement though 

On Thu, Apr 2, 2020 at 2:14 AM Nicholas Jankowski <[hidden email]> wrote:


On Wed, Apr 1, 2020 at 1:31 PM harish3679 <[hidden email]> wrote:
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I've had some trouble even trying to recreate your error.  

1 - did you define some values for d, etc, outside of what you showed? because if I run your script from a clean workspace i get:

enter a value: d
error: 'd' undefined near line 1 column 1
error: called from
    casetest at line 3 column 3

i notice some other things.  You use cross as a variable name, but it is the name of a function for performing a vector cross product.  so if the first switch doesn't set a value to overwrite the meaning of cross, you get an error calling the switch(cross) line. 

do you mean for your switch(y) block to match the letter input d or g?  in that case you need it to be defined as a character with quotation marks,  'd', or "d".  Of course, then it's not clear what values will be going into xpos or xxpos.

look at the help for switch with:  >> help switch





--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India






Hi Harish,

Please use bottom posting in this thread.

The issue you're facing is that you're using a cell array (i.e. xpos is a cell array) in your if statement, so it can't convert a cell array to a logical value (true or false) to decide whether to execute the if or else part of that statement.  I'm really unclear about what the intent of your code is, especially since you redefine variables before doing anything with them.  For example, in your first statement x = ..., what that does is assign the values of d to 3, e to 4, and f to 5, then makes a cell array out of those values. But then, you redefine d on line 5 to be equal to a 1x1 cell array of which contains the value 3. 

Also, in your if statement, you're setting ss equal to the variable xpos?  I don't know what you intend here.  Do you mean to ask if the number the user entered is one of the values in your cell array xpos?  I don't think you're getting anything out of using cell arrays like this.  I'd suggest something more like:

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);

y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
endswitch

Will get you most of the way there, I think.  And, IMO, its a lot more straight forward.

Hope this helps,

James Sherman



Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

harish3679
Thanks for the feedback! 

This is what I am trying to achieve, this is a sample test code for a case operation

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);


y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
    endswitch
    z = input('enter a different value: ');
    switch (z)
      case {xpos, xxpos}
        r = A(2,2)+A(2,1)
      case {xneg, xxneg}
        n = A(3,1)+A(2,1)
       
endswitch

After displaying the results of first switch, I would like to use a different switch whose values are in the range xpos or xxpos it should perform one operation then if
the value is in range of xneg or xxneg it should perform another operation. But since we either chose one case the other case value is not invoked to it throws an error like this

enter a different value: 5
error: 'xxpos' undefined near line 19 column 19
error: called from
    nabbleuserJamessherman at line 18 column 5

On Fri, Apr 3, 2020 at 1:30 AM James Sherman Jr. <[hidden email]> wrote:

On Thu, Apr 2, 2020 at 2:40 AM Harishkumar U <[hidden email]> wrote:
This is what I have done now

x = {d = 3, e = 4, f = 5};
a = {g = 7, h = 2, i = 6};
b = {k = 8, l = 1, m = 9};

d = x(1,1);
e = x(1,2);
f = x(1,3);
g = a(1,1);
h = a(1,2);
i = a(1,3);
k = b(1,1);
i = b(1,2);
m = b(1,3);

y = input('enter a value: ');
switch(y)

case d
  xpos ={d, h, m};
  xneg ={k, h, f};
 
  ss = input('enter cross value: ');
 
case g
  xxpos = {g, h, d};
  xxneg = {f, i, e};
 
  ss = input('enter cross value: ');

endswitch

if (ss = xpos && xxpos)


  max = d+h;
  disp(max)
 
else
 
  min = g-f;
  disp(min)
 
endif

but now, I am getting these errors

enter a value: d
enter cross value: h
error: invalid conversion from cell array to logical value
error: called from
    crossminmax at line 32 column 1

enter a value: d
error: binary operator '==' not implemented for 'cell' by 'scalar' operations
error: called from
    crossminmax at line 16 column 1

On Thu, Apr 2, 2020 at 12:05 PM Harishkumar U <[hidden email]> wrote:
Thank you! I changed cross variable name. Yes, switch y block input should match the output of cross block to obtain either xpos or xneg value. But, when I use 'd' its ignoring the case statement and skipping forward. Can I use If else condition? something similar to switch case statement though 

On Thu, Apr 2, 2020 at 2:14 AM Nicholas Jankowski <[hidden email]> wrote:


On Wed, Apr 1, 2020 at 1:31 PM harish3679 <[hidden email]> wrote:
Hi,

I am trying to use multiple cases for a math operation using case{..,..} as
per octave reference. But unfortunately I am getting an error
'enter a value: d
enter cross value: h
error: 'xxpos' undefined near line 24 column 13
error: called from
    crossminmax at line 22 column 1'


I've had some trouble even trying to recreate your error.  

1 - did you define some values for d, etc, outside of what you showed? because if I run your script from a clean workspace i get:

enter a value: d
error: 'd' undefined near line 1 column 1
error: called from
    casetest at line 3 column 3

i notice some other things.  You use cross as a variable name, but it is the name of a function for performing a vector cross product.  so if the first switch doesn't set a value to overwrite the meaning of cross, you get an error calling the switch(cross) line. 

do you mean for your switch(y) block to match the letter input d or g?  in that case you need it to be defined as a character with quotation marks,  'd', or "d".  Of course, then it's not clear what values will be going into xpos or xxpos.

look at the help for switch with:  >> help switch





--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India






Hi Harish,

Please use bottom posting in this thread.

The issue you're facing is that you're using a cell array (i.e. xpos is a cell array) in your if statement, so it can't convert a cell array to a logical value (true or false) to decide whether to execute the if or else part of that statement.  I'm really unclear about what the intent of your code is, especially since you redefine variables before doing anything with them.  For example, in your first statement x = ..., what that does is assign the values of d to 3, e to 4, and f to 5, then makes a cell array out of those values. But then, you redefine d on line 5 to be equal to a 1x1 cell array of which contains the value 3. 

Also, in your if statement, you're setting ss equal to the variable xpos?  I don't know what you intend here.  Do you mean to ask if the number the user entered is one of the values in your cell array xpos?  I don't think you're getting anything out of using cell arrays like this.  I'd suggest something more like:

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);

y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
endswitch

Will get you most of the way there, I think.  And, IMO, its a lot more straight forward.

Hope this helps,

James Sherman



--
Harish Kumar

M: +91 9488309620, Whatsapp: +91 9944308505

E: [hidden email]

A:

15/36, Ramanna Layout 2, Bharathi Park Road 2, 


Saibaba Colony, Coimbatore -641 043. Tamil Nadu, India







Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

nrjank
Administrator


On Sat, Apr 4, 2020, 12:52 PM Harishkumar U <[hidden email]> wrote:
Thanks for the feedback! 

This is what I am trying to achieve, this is a sample test code for a case operation

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);


y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
    endswitch
    z = input('enter a different value: ');
    switch (z)
      case {xpos, xxpos}
        r = A(2,2)+A(2,1)
      case {xneg, xxneg}
        n = A(3,1)+A(2,1)
       
endswitch

After displaying the results of first switch, I would like to use a different switch whose values are in the range xpos or xxpos it should perform one operation then if
the value is in range of xneg or xxneg it should perform another operation. But since we either chose one case the other case value is not invoked to it throws an error like this

enter a different value: 5
error: 'xxpos' undefined near line 19 column 19
error: called from
    nabbleuserJamessherman at line 18 column 5

Again, for those reading the mailing list, please use bottom posting. 

so you start off with d and g defined by two values from A, so d=3 and g=5

Then you ask the user to put in a value. If they enter a 3, you assign one combination of A elements to xpos and xneg.  If they enter 5, a different assortment of A elements get assigned to xxpps and xxneg. 

Next, if they enter a value that was assigned to xpos or xxpos, you want it to evaluate r, if it was a value assigned to xneg or xxneg, you want it to evaluate n

How can it evaluate xpos, xxpos xneg or xxneg if you never define it?

Why do you use separate x and xx variables? Why not just have it assign to xpos and xneg in the first switch statement? The assignment will change depending in your switch, but the variable names will be consistent.

What happens if someone enters 2 in the first switch? In that case neither Haas been defined and the second switch will break.  You probably want an 'otherwise' statement in each switch.


Reply | Threaded
Open this post in threaded view
|

Re: Multiple case function error

harish3679

Thanks for the tip!! Will use otherwise and see.

On 5 Apr 2020 00:15, "Nicholas Jankowski" <[hidden email]> wrote:


On Sat, Apr 4, 2020, 12:52 PM Harishkumar U <[hidden email]> wrote:
Thanks for the feedback! 

This is what I am trying to achieve, this is a sample test code for a case operation

A = [3 4 5; 7 2 6; 8 1 9];

d = A(1,1);
g = A(2,1);


y = input('enter a value: ');

switch (y)
  case d
    xpos = [A(1,3) A(2,2) A(3,1)]
    xneg = [A(3,1) A(2,2) A(1,3)]
  case g
    xxpos = [A(1,1) A(2,1) A(2,2)]
    xxneg = [A(1,1) A(2,1) A(2,2)]
    endswitch
    z = input('enter a different value: ');
    switch (z)
      case {xpos, xxpos}
        r = A(2,2)+A(2,1)
      case {xneg, xxneg}
        n = A(3,1)+A(2,1)
       
endswitch

After displaying the results of first switch, I would like to use a different switch whose values are in the range xpos or xxpos it should perform one operation then if
the value is in range of xneg or xxneg it should perform another operation. But since we either chose one case the other case value is not invoked to it throws an error like this

enter a different value: 5
error: 'xxpos' undefined near line 19 column 19
error: called from
    nabbleuserJamessherman at line 18 column 5

Again, for those reading the mailing list, please use bottom posting. 

so you start off with d and g defined by two values from A, so d=3 and g=5

Then you ask the user to put in a value. If they enter a 3, you assign one combination of A elements to xpos and xneg.  If they enter 5, a different assortment of A elements get assigned to xxpps and xxneg. 

Next, if they enter a value that was assigned to xpos or xxpos, you want it to evaluate r, if it was a value assigned to xneg or xxneg, you want it to evaluate n

How can it evaluate xpos, xxpos xneg or xxneg if you never define it?

Why do you use separate x and xx variables? Why not just have it assign to xpos and xneg in the first switch statement? The assignment will change depending in your switch, but the variable names will be consistent.

What happens if someone enters 2 in the first switch? In that case neither Haas been defined and the second switch will break.  You probably want an 'otherwise' statement in each switch.