help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multiple case function error


From: Nicholas Jankowski
Subject: Re: Multiple case function error
Date: Sat, 4 Apr 2020 14:44:46 -0400



On Sat, Apr 4, 2020, 12:52 PM Harishkumar U <address@hidden> 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. 
http://www.idallen.com/topposting.html
https://www.caliburn.nl/topposting.html

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 via email to

[Prev in Thread] Current Thread [Next in Thread]