please help with logical operators

if true
fprintf('Enter the old units of volume:\n')
old=input('cubic meter or liter or cubic feet or gallons\n','s');
V=input('Enter the value of volume\n');
fprintf('Enter the new units of volume into which old value os to be converted:\n')
new=input('cubic meter or liter or cubic feet or gallons\n','s');
%old units to cubic meter untis
if length(old)==10
V1=V;
end
if lenght(old)==5
V1=V*(1/1000);
end
if lenght(old)==9
V1=V*(1/35.3146);
end
if length(old)==7
V1=V*(1/264.172);
end
%cubic meter to new desired units
if lenght(new)==10
Vnew=V1;
end
if lenght(new)==5
Vnew=V1*1000;
end
if lenght(new)==9
Vnew=V1*35.3146;
end
if lenght(new)==7
Vnew=V1*264.172;
end
fprintf('The value of volume in the new desired units = %5.2f\n',Vnew)
end
I just keep having a problem with this syntax, this is the error code I get
??? Undefined function or method 'lenght' for input arguments of type 'char'.
do you have any tips on how to approach this with out changing too much the format that I have already , thanks

댓글 수: 3

Matt Kindig
Matt Kindig 2013년 7월 25일
편집: Matt Kindig 2013년 7월 25일
FYI, I don't think this logic will work as you expect. The length of input 'cubic meter' is 11, not 10, so the first condition won't be triggered. Similarly, 'cubic feet' is 10 characters, not 9.
Also, I know you don't want to change the format of the code much, but another way to do this is through switch statements:
switch length(old),
case 10,
Vnew=V1;
case 5,
Vnew=V1*1000;
%etc.
end
Matt Kindig
Matt Kindig 2013년 7월 25일
편집: Matt Kindig 2013년 7월 25일
Or, a vectorized approach, just for your education:
ScaleFactors = [1 1000 35.3146 264.172];
Units = {'cubic meter', 'liter', 'cubic feet', 'gallons'};
fprintf('Enter the old units of volume:\n')
old=input('cubic meter or liter or cubic feet or gallons\n','s');
V=input('Enter the value of volume\n');
fprintf('Enter the new units of volume into which old value os to be converted:\n')
new=input('cubic meter or liter or cubic feet or gallons\n','s');
matchPos = strcmpi(strtrim(old), Units);
V1 = V/ScaleFactors(matchPos); %convert from old units
matchPos = strcmpi(strtrim(new), Units);
Vnew = V1*ScaleFactors(matchPos); %convert to new units
fprintf('The value of volume in the new desired units = %5.2f\n',Vnew)

댓글을 달려면 로그인하십시오.

답변 (1개)

Richard Brown
Richard Brown 2013년 7월 25일

0 개 추천

replace all instances of lenght with length

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2013년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by