Why won't MATLAB see the two strings that I am comparing as equal?
Show older comments
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))
Accepted Answer
More Answers (0)
Categories
Find more on Image Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!