Options to use the switch case in matlab

조회 수: 3 (최근 30일)
Jes
Jes 2017년 10월 22일
답변: Image Analyst 2017년 10월 22일
I am writing a program for adding and remove employee details using switch case statement.
Like the following
n=input('Enter the choice 1. Add 2. Remove');
switch n
case 1
Some operations
case 2
some operations
otherwise
fprintf('The choice does not exist. Please try again\n');
n=input('Enter the choice 1. Add 2. Remove');
end
i need a help in the above code. If the users enters the choice greater than 2, I should go back and execute the newly selected options( 1 or 2). Could you please help me with some options? Thanks
  댓글 수: 1
dpb
dpb 2017년 10월 22일
I'd suggest a couple alternatives...
  1. Limit the input to the range of the SWITCH statement in the section of code getting input before the construct; this lowers the complexity of the code structure having to encompass the whole thing,
  2. Consider LISTDLG as a way to accomplish 1) above...the user is given only the list of allowable choices from which to choose and includes a description as well as the visual clue rather than the text listing of options...

댓글을 달려면 로그인하십시오.

채택된 답변

KL
KL 2017년 10월 22일
try this,
n = 3;
while(n~=1 || n~=2)
n=input('Enter the choice 1. Add 2. Remove');
switch n
case 1
Some operations
case 2
some operations
otherwise
fprintf('The choice does not exist. Please try again\n');
end
end
  댓글 수: 1
Jes
Jes 2017년 10월 22일
Thanks a lot! Exactly what I need.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 10월 22일
Don't do that. Why make it way more complicated than it needs to be? Use questdlg() instead.
buttonText = questdlg('Enter your choice', titleBarCaption, 'Add', 'Remove', 'Add');
if strcmpi(buttonText, 'Add')
% Code to add.
else
% Code to remove.
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by