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

但是,您没有清楚地解释您要寻找的内容,如何使用函数如下:

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文件中并运行。

您需要告诉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