Question

I need to check each element of a matrix to see if it's something specific or not. I have to use switch/case for this piece of code. I have a vector like this :

COUNTER=[counterA counterB counterC counterWaste]

I tried this :

for n=1:size(COUNTER,2)
   switch COUNTER(1,n)
       case counterA
          disp(['counterA is ' , num2str(counterA)])
       case counterB
          disp(['counterB is ' , num2str(counterB)])
       case counterC
          disp(['counterC is ' , num2str(counterC)])
       case counterWaste
          disp(['counterWaste is ' , num2str(counterWaste)])
    end
end

counterA & counterB & counterC & counterWaste are some variables which contain a random number. It gived the right number as I already know the right number of each counter variable, but the strings are not correct. for example it once it gives

counterA is 1
counterB is 2
counterA is 1
counterB is 2

next time

counterA is 4
counterB is 1
counterB is 1
counterWaste is 0

and ...

I don't know where the problem is from. anybody has any idea ?

Was it helpful?

Solution

You're misusing case, friend. You're trying to switch by the variable name, but the variable name simply points to value that variable holds. Try this:

for n=1:size(COUNTER,2)
   switch n
       case 1
          disp(['counterA is ' , num2str(counterA)])
       case 2
          disp(['counterB is ' , num2str(counterB)])
       case 3
          disp(['counterC is ' , num2str(counterC)])
       case 4
          disp(['counterWaste is ' , num2str(counterWaste)])
    end
end

Note that I'm switching by a value that behaves predictably. Good luck, and happy coding.

Edit: Just to clarify, I don't think this is the best solution, Raf's is better. It just meets the asker constraint of using switch/case.

OTHER TIPS

Could you drop the for and switch statements and do it like this?

disp(['counterA is ' , num2str(COUNTER(1,1))]);
disp(['counterB is ' , num2str(COUNTER(1,2))]);
disp(['counterC is ' , num2str(COUNTER(1,3))]);
disp(['counterWaste is ' , num2str(COUNTER(1,4))]);

I think the problem is that you have

COUNTER=[counterA counterB counterC counterWaste]

but counterA counterB counterC counterWaste are, as you say, random values, so in your first example

counterA is 1
counterB is 2
counterA is 1
counterB is 2

you have, I imagine COUNTER=[1 2 1 2] And you use a swich case on the values inside COUNTER, but you have counterA = counterC = 1 here for example, so your switch case is used like that

for n=1:size(COUNTER,2)
   switch COUNTER(1,n)
       case counterA  % counterA = counterC = 1 go there
          disp(['counterA is ' , num2str(counterA)])
       case counterB  % counterB = counterWaste = 2 go there
          disp(['counterB is ' , num2str(counterB)])
       case counterC  % counterC = 1 but never used because counterC go to case counterA
          disp(['counterC is ' , num2str(counterC)])
       case counterWaste % same as case counterC
          disp(['counterWaste is ' , num2str(counterWaste)])
    end
end

Actually I think every case should have a different value (case are evaluating the values, not the var name) because if two case have the same value (It is possible here because your values are random) only the first one is evaluated (:

The problem is the same for:

counterA is 4
counterB is 1
counterB is 1
counterWaste is 0

You have COUNTER=[4 1 1 0] so case counterC is never evaluated because case counterB is, for both counterB and counterC. But I may be wrong, long time not used MatLab

I think this should work:

for n=1:size(COUNTER,2)
   switch n
       case 1  
          disp(['counterA is ' , num2str(counterA)])
       case 2  
          disp(['counterB is ' , num2str(counterB)])
       case 3  
          disp(['counterC is ' , num2str(counterC)])
       case 4 
          disp(['counterWaste is ' , num2str(counterWaste)])
    end
end

Note that there are many other ways to do so. This one is quite complicated for nothing ^^

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top