嵌套开关控制流程在MATLAB中

时间:2017-04-25 16:02:06

标签: matlab

你可以建议!!!场景如下所示:我尝试了多个切换,case语句,但无法进行..

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

2 个答案:

答案 0 :(得分: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文件中并运行它。

答案 1 :(得分: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