2010-07-27 3 views
0

Vous n'aurez pas besoin de lire tout ce code, mais je l'ai posté pour référence. J'ai besoin de changer le segment de code suivant des cas 0-8 juste pour n. La seule chose qui change entre les cas est la plage de z et les noms des variables (msgLength0, msgLength1, msgLength2, etc.) Je me bats. J'ai essayé de mettre les variables msgLength # dans un tableau mais je ne peux pas l'obtenir pour assigner les données aux lignes correctes sans les variables "count".Matlab: Se débarrasser d'un "Switch/Case" dans mon code

Comment puis-je me débarrasser de ce "commutateur" et utiliser des variables au lieu de définir séparément msgLength # dans des cas individuels?

for these folders 
    for these files 
     countmsgLength0= 1; 
     countmsgLength1= 1; 
     countmsgLength2= 1; 
     countmsgLength3= 1; 
     countmsgLength4= 1; 
     countmsgLength5= 1; 
     countmsgLength6= 1; 
     countmsgLength7= 1; 
     countmsgLength8= 1; 
     for x= 1:length(firstinSeq) 
     for y= 1:length(littledataPassed) 
      if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2) 
      switch firstinSeq(x,3) 
      case 0 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == 0),1,'first'); 
       if isempty(indexProcess) 
        msgLength0(countmsgLength0,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];     
       else msgLength0(countmsgLength0,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength0= countmsgLength0 + 1; 
      case 1 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == 0), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength1(countmsgLength1,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength1(countmsgLength1,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength1= countmsgLength1 + 1; 
      case 2 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z= 0:1 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength2(countmsgLength2,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength2(countmsgLength2,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength2= countmsgLength2 + 1; 
       end 
      case 3 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z= 0:2 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength3(countmsgLength3,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength3(countmsgLength3,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength3= countmsgLength3 + 1; 
       end 
      case 4 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z=0:3 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength4(countmsgLength4,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength4(countmsgLength4,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength4= countmsgLength4 + 1; 
       end 
      case 5 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z= 0:4 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength5(countmsgLength5,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength5(countmsgLength5,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength5= countmsgLength5 + 1; 
       end 
      case 6 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z= 0:5 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength6(countmsgLength6,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength6(countmsgLength6,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength6= countmsgLength6 + 1; 
       end 
      case 7 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z=0:6 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength7(countmsgLength7,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength7(countmsgLength7,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength7= countmsgLength7 + 1; 
       end 
      case 8 
       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z=0:7 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength8(countmsgLength8,:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength8(countmsgLength8,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength8= countmsgLength8+ 1; 
       end    
      end 
      break 
      end 
     end 
     end 
     msgLength0((countmsgLength0):100,:)= []; 
     msgLength1((countmsgLength1):1000,:)= []; 
     msgLength2((countmsgLength2):1000,:)= []; 
     msgLength3((countmsgLength3):1500,:)= []; 
     msgLength4((countmsgLength4):1000,:)= []; 
     msgLength5((countmsgLength5):1000,:)= []; 
     msgLength6((countmsgLength6):500,:)= []; 
     msgLength7((countmsgLength7):300,:)= []; 
     msgLength8((countmsgLength8):300,:)= []; 
     allmsgLength0(countallmsgLength0:(countallmsgLength0+length(msgLength0(:,1))-1),:) = msgLength0; 
     allmsgLength1(countallmsgLength1:(countallmsgLength1+length(msgLength1(:,1))-1),:) = msgLength1; 
     allmsgLength2(countallmsgLength2:(countallmsgLength2+length(msgLength2(:,1))-1),:) = msgLength2; 
     allmsgLength3(countallmsgLength3:(countallmsgLength3+length(msgLength3(:,1))-1),:) = msgLength3; 
     allmsgLength4(countallmsgLength4:(countallmsgLength4+length(msgLength4(:,1))-1),:) = msgLength4; 
     allmsgLength5(countallmsgLength5:(countallmsgLength5+length(msgLength5(:,1))-1),:) = msgLength5; 
     allmsgLength6(countallmsgLength6:(countallmsgLength6+length(msgLength6(:,1))-1),:) = msgLength6; 
     allmsgLength7(countallmsgLength7:(countallmsgLength7+length(msgLength7(:,1))-1),:) = msgLength7; 
     allmsgLength8(countallmsgLength8:(countallmsgLength8+length(msgLength8(:,1))-1),:) = msgLength8; 
     countallmsgLength0 = countallmsgLength0 + length(msgLength0(:,1)); 
     countallmsgLength1 = countallmsgLength1 + length(msgLength1(:,1)); 
     countallmsgLength2 = countallmsgLength2 + length(msgLength2(:,1)); 
     countallmsgLength3 = countallmsgLength3 + length(msgLength3(:,1)); 
     countallmsgLength4 = countallmsgLength4 + length(msgLength4(:,1)); 
     countallmsgLength5 = countallmsgLength5 + length(msgLength5(:,1)); 
     countallmsgLength6 = countallmsgLength6 + length(msgLength6(:,1)); 
     countallmsgLength7 = countallmsgLength7 + length(msgLength7(:,1)); 
     countallmsgLength8 = countallmsgLength8 + length(msgLength8(:,1)); 
    end 
    cd .. 
end 
+0

Je ne comprends pas très bien ce que vous essayez d'atteindre. pourriez-vous expliquer plus en détail et/ou donner un exemple dépouillé pour illustrer ce que vous voulez – second

Répondre

0

Voici un moyen de se débarrasser de l'interrupteur/boîtier. Cependant, cela peut vous être plus utile si vous avez expliqué davantage à quoi ressemblent vos données et ce que vous souhaitez réaliser. Très probablement, toute cette analyse peut être beaucoup simplifiée.

countmsgLength = ones(9,1); %# countmsgLength(1) are the counts for to case 0 
msgLength = cell(9,1); 


for x= 1:length(firstinSeq) 
     for y= 1:length(littledataPassed) 
      if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2) 
      switchNum = firstinSeq(x,3); %# switchNum is 0...8 

       indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first'); 
       for z= 0:switchNum-1 %# if switchNum <2, the loop will execute once 
       indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ... 
        littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ... 
        littledataPassed(y:y+indexStop-1,5) == z), 1, 'first'); 
       if isempty(indexProcess) 
        msgLength{switchNum+1}(countmsgLength(switchNum+1),:)= [firstinSeq(x,:) [0 0 0 0 0 0]]; 
       else msgLength{switchNum+1}(countmsgLength(switchNum+1),:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)]; 
       end 
       countmsgLength(switchNum+1)= countmsgLength(switchNum+1) + 1; 
       end %# loop z 
      end %# if 
     end %# loop y 
end %# loop x 
0

Pourriez-vous essayer d'utiliser le tableau pour countMsgLength, telle qu'au lieu de, par exemple, vous countMsgLength4 incrémenter countMsgLength(4). Puisque msgLengthN semble être des matrices, vous pourriez former un tableau 3d, qui serait comme un cube, si tous les msgLengthN finissent par avoir la même dimension. Vous avez juste besoin d'initialiser ce tableau au début, puis définissez les éléments appropriés comme le progresse du code. Alternativement, si msgLengthN sont de dimensions différentes, vous pouvez utiliser un tableau de cellules, en réglant msgLength{N}(countmsgLength(N),:) = au lieu de msgLengthN(countmsgLengthN,:) comme vous le faites ci-dessus. Juste avant chaque utilisation, les initialiser à une matrice vide, par exemple, au début faire

msgLength = {}; 
for k=1:8 
    msgLength{k} = []; 
end 

ou, si vous connaissez des dimensions à l'avance, à une matrice zéro

msgLength = {}; 
for k=1:8 
    msgLength{k} = zeros([rows cols]); 
end 

Autre que cela, sans explication supplémentaire, il semble difficile de déchiffrer ce que vous essayez de faire.

6

La réponse est, NE PAS utiliser des variables comme celles-ci, avec des nombres intégrés dans les noms. Cela vous oblige à écrire du code désordonné comme celui-ci. Plus tard, vous vous retrouverez à essayer désespérément de déboguer ce code. Finalement, vous trouverez l'erreur, probablement dans une erreur de copier/coller, où vous avez tapé un nom de variable incorrectement numéroté. A la place, utilisez un vecteur, un tableau de cellules, une structure, etc. Stockez vos informations dans des tableaux, puis indexez dans ce tableau/vecteur/cellule/struct. Votre code deviendra plus simple, plus facile à déboguer, plus court.

+0

Merci. Je suis nouveau au matlab et à la programmation en général, mais le consensus semble être que c'est la meilleure voie à suivre. – Maxwell

Questions connexes