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 Commento

Cedric
Cedric il 29 Apr 2013
Modificato: Cedric il 29 Apr 2013
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.

Accedi per commentare.

Categorie

Scopri di più su Particle & Nuclear Physics in Centro assistenza e File Exchange

Richiesto:

il 29 Apr 2013

Community Treasure Hunt

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

Start Hunting!

Translated by