Why do I get an error?

1 回表示 (過去 30 日間)
Eyad Alharbi
Eyad Alharbi 2018 年 3 月 10 日
編集済み: Eyad Alharbi 2018 年 3 月 10 日
Hi I was writing a script for a program that ask the user to enter the material of an object and the surface's material to use the friction consent to find the force, and here is my script :
object=input('Enter the matiral of the object : ','s');
surface=input('Enter the matiral of the surface : ','s');
W=input('Enter weight : ');
U=[0.2 0.4 0; 0 0.35 0 ; 0 0 0.7];
%calculation_section
if object=='metal'
OB=1;
elseif object=='wood'
OB=2;
elseif object =='rubber'
OB=3;
elseif surface=='metal'
S=1;
elseif surface=='wood'
S=2;
elseif surface=='concrete'
S=3;
end
F=U(OB,S);
%output_section
disp(['The force is :',num2str(F)]);
The error i receive comes from different lines sometimes from this line OB=1; and sometimes from this line F=U(OB,S) each time I hit run it gives me a different error massage. I am stuck in this error for two days, I searched a lot but I couldn't fix it.I hope if you guys can help me.
  2 件のコメント
David Fletcher
David Fletcher 2018 年 3 月 10 日
you should note that object=='metal' will not return a logical scaler: instead it returns a 1x5 logical vector correspnding to a match (or not) in each of the character positions. If the things you are testing are of a different lengths then you will get an error. Use a string comparison function instead
Geoff Hayes
Geoff Hayes 2018 年 3 月 10 日
See strcmp or strcmpi for details.

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

採用された回答

Jan
Jan 2018 年 3 月 10 日
Instead of a bunch of if statements with strcmp, you can use switch also:
switch object
case 'metal'
OB = 1;
case 'wood'
OB = 2;
case 'rubber'
OB = 3;
otherwise
error('Unexpected object.')
end
switch surface
case 'metal'
S = 1;
case 'wood'
S = 2;
case 'concrete'
S = 3;
otherwise
error('Unexpected surface.')
end
In addition you need two different if or switch blocks. In your case after an object was found, the surface is not examined anymore.

その他の回答 (1 件)

Eyad Alharbi
Eyad Alharbi 2018 年 3 月 10 日
編集済み: Eyad Alharbi 2018 年 3 月 10 日
thank you guys I tried strcmpi and it worked.

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by