Error using == matrix dimensions must agree

I am very new to MATLAB and started going through some sample problems.
I wrote some simple code, however, I want it to display "'lease enter inches or cent" if you don't enter 'in' or 'cm'. If I enter a value that is more than 2 characters I get an error:
disp('This program will calculate the volume of a pyramind.')
length = input('Enter the length of the base: ');
width = input('Enter the width of the base: ');
height = input('Enter the height of the pyramid: ');
unit = input('Are these measurements in inches ''in'' or centimeters ''cm'': ','s');
if unit == 'in'
volum_in = 1/3*length*width*height;
fprintf('The volume of the pyramid is %3.3f\n',volum_in)
elseif unit == 'cm'
volum_cm = 1/3*length*width*height*2.54^3;
fprintf('The volume of the pyramid is %3.3f\n',volum_cm)
else
disp('Please enter inches or cent')
end

1 件のコメント

Cedric
Cedric 2013 年 4 月 29 日
編集済み: Cedric 2013 年 4 月 29 日
Tu understand why you have to use methods proposed by Walter and Azzi, execute the following from the command line (without the >> that represent the prompt):
>> unit = input('Are these measurements in inches ''in'' or centimeters ''cm'': ','s');
and type " if " (just the two charcaters i and f)
Then evaluate the first condition from your IF statement:
>> unit == 'in'
ans =
1 0
As you can see, the result is an array of booleans, which means that the == compares arrays. These arrays have to have the same size and the outcome of the comparison is not exactly the unique boolean that you need to build the IF statement.

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

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 4 月 29 日

2 投票

Use strcmp() to compare strings.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 29 日

0 投票

Use
If isequal(unit,'cm')

カテゴリ

ヘルプ センター および File Exchange で Particle & Nuclear Physics についてさらに検索

タグ

質問済み:

2013 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by