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
Rena Berman 2022 年 2 月 3 日

(Answers Dev) Restored edit

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

回答 (1 件)

DGM
DGM 2022 年 2 月 2 日

0 投票

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
Rahim Parvez 2022 年 2 月 2 日
When I try to implement the argument before the main switch-case structure, the function fails to run?
DGM
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
Rahim Parvez 2022 年 2 月 2 日
So how can I create an argument that recognizes "lb" as one of the few accepted answers of the input? Forgive my inexperience, MATLAB is still very new to me.
DGM
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 ExchangeCharacters and Strings についてさらに検索

質問済み:

2022 年 2 月 2 日

編集済み:

2022 年 2 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by