Using if statement for switch
이전 댓글 표시
function [success, Out_Torque] = convertTorque(In_Torque, UnoF, UnoL, DesF, DesL )
success=0;
Out_Torque=0;
forceconv=0;
lengthconv=0;
switch UnoF
case "N"
switch DesF
case "N"
forceconv= 1;
case "lb"
forceconv=(1/(0.453592 * 9.80665));
case "oz"
forceconv=(16/(0.453592 * 9.80665));
case "kN"
forceconv=(1/1000);
end
case "kN"
switch DesF
case "N"
forceconv= 1000;
case "lb"
forceconv= (1000/(0.453592 * 9.80665));
case "oz"
forceconv= 16 * (1000/(0.453592 * 9.80665));
case "kN"
forceconv= 1;
end
case "lb"
switch DesF
case "N"
forceconv = (0.453592 * 9.80665);
case "lb"
forceconv = 1;
case "oz"
forceconv= 16;
case "kN"
forceconv= (0.453592 * 9.80665)/1000;
end
case "oz"
switch DesF
case "N"
forceconv = (0.453592 * 9.80665)/ 16;
case "lb"
forceconv = 1/16;
case "oz"
forceconv = 1;
case "kN"
forceconv= (0.453592 * 9.80665)/(16*1000);
end
end
switch UnoL
case "ft"
switch DesL
case "ft"
lengthconv= 1;
case "in"
lengthconv= 12;
case "mm"
lengthconv= (12*25.4);
case "m"
lengthconv= (12*25.4)/1000;
end
case "in"
switch DesL
case "ft"
lengthconv= 1/12;
case "in"
lengthconv= 1;
case "mm"
lengthconv= 25.4;
case "m"
lengthconv= 25.4/1000;
end
case "mm"
switch DesL
case "ft"
lengthconv= 1/(12*25.4);
case "in"
lengthconv= 1/25.4;
case "mm"
lengthconv= 1;
case "m"
lengthconv= 1/1000;
end
case "m"
switch DesL
case "ft"
lengthconv= (1000/(12*25.4));
case "in"
lengthconv= (1000/ 25*4);
case "mm"
lengthconv= 1000;
case "m"
lengthconv= 1;
end
end
% if ((UnoF ~= "N" || "lb" || "oz" || "kN") || (UnoL ~= "ft" || "in" || "mm" || "m") )
% Out_Torque=0;
% success=0;
% end
Out_Torque= In_Torque * forceconv * lengthconv;
success=1;
I'm having trouble creating a check for the input on my function. I want the function to return status=0 and Out_Torque= 0 if anything other than "N" "lb" "oz" "kN" is entered for Units of Force and likewise for "ft" "in" "mm" "m" for Units of Length.
The commented out part at the end is what I've been able to come up but it doesn't work and I know there's an easier way to do this.
댓글 수: 1
Rena Berman
2022년 2월 3일
(Answers Dev) Restored edit
답변 (1개)
DGM
2022년 2월 2일
This is a simplified conceptual example of one way.
% define a list of valid strings
validforcestr = lower(["N" "lb" "oz" "kN"]);
% if this argument isn't valid, return with an error message stating why
if ~ismember(lower(Unof),validforcestr)
error('some error message about Unof')
end
You'd have to do similar for both force arguments and both length arguments. If you want it to be case-sensitive, omit the usage of lower().
If for some reason you want it to return null results without error, simply replace the error() call with output assignment and return.
% define a list of valid strings
validforcestr = lower(["N" "lb" "oz" "kN"]);
% if this argument isn't valid, return with an error message stating why
if ~ismember(lower(Unof),validforcestr)
Out_Torque = 0;
success = 0;
return;
end
Obviously, this has to happen before the main switch-case structure.
댓글 수: 4
Rahim Parvez
2022년 2월 2일
DGM
2022년 2월 2일
That's because the logical tests you were using don't work. This
if ((UnoF ~= "N" || "lb" || "oz" || "kN") || (UnoL ~= "ft" || "in" || "mm" || "m") )
is either true or will throw an error because this
UnoF ~= "N" || "lb" || "oz" || "kN"
is the same as this
(UnoF ~= "N") || "lb" || "oz" || "kN"
and "lb" is not a logical value, nor can it be interpreted as one.
Putting the input validation after the inputs are used defeats the purpose entirely.
Rahim Parvez
2022년 2월 2일
DGM
2022년 2월 2일
I already did. The example I gave above uses ismember() to check whether the specified argument matches one of the strings in the array called validforcestr.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!