MATLAB: I have the user input a string, then I try and use the string in an if statment, but matlab keeps telling me the matrix dimensions don’t agree, and I don’t have a matrix

if-statementsinputstrings

planet = input('What planet do you want to visit? ','s');
weight = input('Great! How much do you weigh on Earth? ');
if planet strcmp ('Mercury')
new_weight = .37 * weight
elseif planet == 'Venus'
new_weight = .90 * weight
elseif planet == 'Earth'
fprintf('You''ve already been to Earth!')
elseif planet == 'Moon'
new_weight = .17 * weight
elseif planet == 'Mars'
new_weight = .38 * weight
elseif planet == 'Jupiter'
fprintf('This is a gas giant, you might not be able to stand on it!')
new_weight = 2.65 * weight
elseif planet == 'Saturn'
fprintf('This is a gas giant, you might not be able to stand on it!')
new_weight = 1.13 * weight
elseif planet == 'Uranus'
fprintf('This is a gas giant, you might not be able to stand on it!')
new_weight = 1.09 * weight
else
fprintf('This is a gas giant, you might not be able to stand on it!')
new_weight = 1.43 * weight
end

Best Answer

In MATLAB, string variables are handled as vectors.
With respect to the errors, you got close with your test for Mercury. See if:
if strcmp(planet, 'Mercury')
helps. If so, do the same for the others.