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