2017-04-25 87 views
2

可以請你建議!該方案是象下面這樣:我嘗試了多個開關,case語句,但不能做..在MATLAB中嵌套開關控制流程

switch 1 
case 'YESS' 
{ 
% curly braces are just to denote the scope of case 'YESS' 
.....code  
switch 2 
case 'Yes' 
from here Can I jump again to the start of switch(1),case 'YESS' ?? 
case 'No' 
%% some message 
end 
} 
case'NOO' 
%% some message 
end 

回答

1

但是你沒有解釋清楚你在找什麼,怎麼樣使用功能如下:

function main 
prompt = 'Do you want more? Y/N [Y]: '; 
str1 = askYesNoQuestion(prompt); 
switch str1 
    case 'Y' 
     prompt2 = 'Asking to make sure? Y/N [Y]: '; 
     str2 = askYesNoQuestion(prompt2); 
     disp(str2); 
    case 'N' 
     disp('OK no problem!'); 
end 
end 


function str = askYesNoQuestion(prompt) 
str = input(prompt,'s'); 
if isempty(str) 
    str = 'Y'; 
end 
switch str 
    case 'Y' 
     disp('you said yes'); 
    case 'N' 
     disp('you said no') 
end 
end 

您可以將整個代碼保存在名爲main.m的m文件中並運行它。

+0

會有的隔音當然不同的兩個文件夾類別(說機器聲音和人聲)。我一次提取文件夾中所有文件的特徵,將特徵保存在表格矩陣中,如果用戶說是,則提示消息框要求下一個文件夾進行特徵提取。我必須重複第一個文件夾的步驟......它是確切的場景。我能夠爲第一個文件夾做到這一點,但不能到第二個文件夾...期待一些幫助! –

0

你需要告訴MATLAB不斷循環

keepLooping = true; 
    while keepLooping 
    switch 1 
    case 'YESS' 
    keepLooping = false; %% exit switch 1 

     switch 2 
     case 'Yes' %% back to switch 1 
     keepLooping = true; %% re-enter switch 1 

     case 'No' %% some message 
     keepLooping = false %% exit switch 1 
     end 
    case'NOO' %% some message 
    keepLooping = false; %% exit switch 1 
    end 
    end 

或替代,已經跳「YESS」盒內:

isYesOrNo = 'YESS' 
     keepLooping = true; 
     while keepLooping 
     switch isYesOrNo 
     case 'YESS' 
     keepLooping = false; %% exit switch 1 
      switch 2 
      case 'Yes' %% back to switch 1 
      keepLooping = true; %% re-enter switch 1 
      isYesOrNo = 'YESS' %% re-enter 'YESS' 

      case 'No' %% some message 
      keepLooping = false %% exit switch 1 
      end 
     case'NOO' %% some message 
     keepLooping = false; %% exit switch 1 
     end 
     end