Help with For/If commands

조회 수: 3 (최근 30일)
Jevin
Jevin 2012년 4월 18일
I am working on a project right now, and have a menu asking what a user would like to solve for. The menu corresponds to function handles, and in the function handles the variables can be used in more than 1. What i want to do is to have it run for variable A or B or C, and if it doesn;t have A B or C in it, it will stop and move to D E and F.
Here is what some of the code looks like :
(choice is the variable i assigned for the menu)
if choice == 1,4;6;7;8;10;11;
a=input ('What is the cross sectional area? ');
else
end
if choice ==2,3;5;6;
cl=input('What is the change in length? ');
else
end

채택된 답변

Junaid
Junaid 2012년 4월 18일
Use switches .. I hope it will solve your problem.
ex.
switch choice
case {1,4,6,7,8,10,11}
% your code here
case {2,3,5,6}
% your code here
end
  댓글 수: 1
Jevin
Jevin 2012년 4월 18일
That seems to work at first, but later on i have something along the lines of :
if choice == 2||3||4||5||6||7;
l=input('What is the length of your object? ');
end
if choice == 8||9||11;
cp=input('How many points of contact does your object have? ');
end
if choice == 3||4||6||7;
em=input('What is the elastic modulus of your material? ');
so it needs to work for more than one, since i am asking the components of formulas, and 5 of the formulas share 1 variable etc.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 4월 18일
switch choice
case {2, 3, 4, 5, 6, 7}
%your code here
end
switch choice
case {8, 9, 11}
%your code here
end
If you prefer to use "if":
if ismember(choice, [1 4 6 7 8 10 11])
%your code here
end
if ismember(choice, [8, 9, 11])
%your code here
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by