Why won't MATLAB see the two strings that I am comparing as equal?

조회 수: 2 (최근 30일)
Zach
Zach 2013년 12월 7일
댓글: sixwwwwww 2013년 12월 7일
I am doing this as an assignment for my highschool MATLAB course. I cannot understand why MATLAB does not see my volume ('v') as equal to one of the volumes in my if structure. I even changed one of the if statements to use strcmp() instead of isequal(), to no avail. I run the script and enter the information, and no matter what I enter for the volume it outputs my 'else' code and gives me this error :
Choose a material(pine, oak, maple, yew, willow, mahogany, cypress, birch, or walnut) oak
Mass units to be used (kg, g, mg): kg
Mass units to be used (m^3, cm^3,mm^3, L, or mL): L
Inputted volume units are invalid.
Undefined function or variable 'p2'.
(I hope I entered the code correctly)
if true
% code
endmat = input('Choose a material(pine, oak, maple, yew, willow, mahogany, cypress, birch, or walnut) ','s');
%
%
% m = input('Mass units to be used (kg, g, mg): ','s');
% v = input('Mass units to be used (m^3, cm^3,mm^3, L, or mL): ','s');
%
% v = lower(v);
%
% % Process
% % densities in kg/m^3
% switch mat
% case 'pine'
% p = 500;
% case 'oak'
% p = 600;
% case 'maple'
% p = 750;
% case 'yew'
% p = 670;
% case 'willow'
% p = 450;
% case 'mahogany'
% p = 850;
% case 'cypress'
% p = 510;
% case 'birch'
% p = 670;
% case 'walnut'
% p = 700;
% otherwise
% disp('Either the material you entered is not in the database, or your query is misspelled.')
% end
%
% if isequal(m,'kg')
% p1 = p;
% elseif isequal(m,'g')
% p1 = p*10^3;
% elseif isequal(m,'mg')
% p1 = p*10^6;
% else
% disp('Inputted mass units are invalid.')
% end
%
%
% if strcmp(m,'m^3')
% p2 = p1;
% elseif isequal(m,'cm^3')
% p2 = p1*100^3;
% elseif isequal(m,'mm^3')
% p2 = p1*1000^3;
% elseif isequal(m,'l')
% p2 = p1*1000;
% elseif isequal(m,'ml')
% p2 = p1*10^6;
% else
% disp('Inputted volume units are invalid.')
% end
%
%
% % Output
% disp(sprintf('The density of %s is %d %s/%s.',mat,p2,m,v))

채택된 답변

sixwwwwww
sixwwwwww 2013년 12월 7일
replace
m with v
in your last if-else structure as follow:
if isequal(v,'m^3')
p2 = p1;
elseif isequal(v,'cm^3')
p2 = p1*100^3;
elseif isequal(v,'mm^3')
p2 = p1*1000^3;
elseif isequal(v,'l')
p2 = p1*1000;
elseif isequal(v,'ml')
p2 = p1*10^6;
else
disp('Input volume units are invalid.')
end
  댓글 수: 2
Zach
Zach 2013년 12월 7일
Well aren't I observant... XP Thanks for the help! I guess sometimes you just need an outside set of eyes!
sixwwwwww
sixwwwwww 2013년 12월 7일
hahhahaha. sometimes it happens to everyone especially in programming. so don't worry at all and keep programming

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by