Switch-case syntax: Jump to n-th case?

6 ビュー (過去 30 日間)
Sokratis Panagiotidis
Sokratis Panagiotidis 2022 年 9 月 24 日
編集済み: dpb 2022 年 9 月 26 日
Hi all,
I am now having some issues with a problem that uses a switch-case syntax to execute certain orders. Now whenever I enter a case by simply entering the number needed, I often select a certain area inside a figure and then I confirm that by pressing enter, so basically this:
for i = 1:inf
% Infinte for-loop since it is not known how many exact times the user needs to execute these orders.
% Can simply end this by pressing '0' when asked for which case.
prompt = 'Please input letter code: ';
inp = input(prompt);
switch inp
case 1
brush on % To select the area inside the plot/figure.
prompt2 = 'Please press a key after finishing the brush: ';
inp2 = input(prompt2);
%% Execute order inside case 1.
case 2
brush on % To select the area inside the plot/figure.
prompt2 = 'Please press a key after finishing the brush: ';
inp2 = input(prompt2);
%% Execute order inside case 2.
case 3
% Executes an order after having selected the areas in case 1 and case 2.
% Which one exactly is not relevant to the problem.
end
%% End the programm by pressing '0'. It wouldn't work if used as a case, thus I used an if-statement.
if inp == 0
return
end
end
Now what I'd like to do is to simply make it easier for the user to work with the programm. Otherwise the user has to press: 1 -> Enter -> 2 -> Enter -> 3, just to simply make a single move. But since the user often will have up to 10 moves to execute, this could become really annoying considering there's also multiple figures to select.
So what I'd like is to simply select wether the user wants to confirm the selected areas or try again, after having executed the order inside case 2.
So it would look a bit like this:
for i = 1:inf
% Infinte for-loop since it is not known how many exact times the user needs to execute these orders.
% Can simply end this by pressing '0' when asked for which case.
prompt = 'Please input letter code: ';
inp = input(prompt);
switch inp
case 1
brush on % To select the area inside the plot/figure.
prompt2 = 'Please press a key after finishing the brush: ';
inp2 = input(prompt2);
%% Execute order inside case 1.
case 2
brush on % To select the area inside the plot/figure.
prompt2 = 'Please press a key after finishing the brush: ';
inp2 = input(prompt2);
%% Execute order inside case 2.
if inp == 3
% Confirm your previous choices
else
% Delete your choices of selected areas and jump back to
% case 1, without having to press '1'.
end
end
end
How would one jump to case 1 while being inside case 2? This way I can only press a different key to once again having to select '1'. I'd like to skip the pressing of '1'.
Thanks in advance!
  3 件のコメント
dpb
dpb 2022 年 9 月 24 日
編集済み: dpb 2022 年 9 月 24 日
Case functionality would need to be functions that can be called from wherever -- with exit flags to return from/break the loop when needed.
%% End the programm by pressing '0'. It wouldn't work if used as a case, thus I used an if-statement.
if inp == 0
return
end
The above is also not true --
case 0
break
followed by
return
after the loop also works -- good if there needs be other cleanup first before quitting...othewise simply
case 0
return
immediately leaves.
Also as stylistic form in MATLAB syntax, instead of
for i = 1:inf
...
for an indefinite loop, use
while 1
...

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2022 年 9 月 24 日
Why not let them enter all the numbers in a single input statement
userInput = input('Enter one or more numbers : ', 's')
numbers = userInput - '0' % A double vector, not character array.
Enter one or more numbers : 12340
userInput =
'12340'
numbers =
1 2 3 4 0
  5 件のコメント
dpb
dpb 2022 年 9 月 25 日
編集済み: dpb 2022 年 9 月 26 日
This is probably a place for application of the idea of a a state machine -- walking from one action to the next.
An example for a Mealy and Mealy-Moore state machine code structure in MATLAB code is <Model-a-state-machine>. It was set up specifically for HDL code generation but gives one the feel for organizing such a sequential state of steps that progress from one to the next. The whole thing is cast into the one subroutine and it is what is called in the infinite loop which handles input/output to separate the two.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by