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
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
2013년 4월 29일
2 개 추천
Use strcmp() to compare strings.
카테고리
도움말 센터 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!