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

1 次查看(过去 30 天)
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