MATLAB: Problem over switch function

switchvan der waal

So this is what I have so far.
and here is the problem:
I've made the switches, and when I go to HELP SWITCH, it shows me how to do the switch, but I don't really understand how to integrate it within the problem. I was trying to figure out if there was a way to input the element needed, and then it would add the values of a and b, and then I could execute the equation, but I can't really figure out how to go about doing that (if I'm right).

Best Answer

Your code works, but you have to input the element as:
'he'
(with the quotation marks), not just
he
which Matlab tries to evaluate as a function or variable rather than a string.
If you want to get fancy you can also use
validatestring
to ensure a sensible input string (this also allows you to input, for example just 'o' if one of your defined valid strings is 'oxygen' and it will convert 'o' to 'oxygen' on output, though, of course, just 'h' would be ambiguous.
By the way, your spelling of 'Hydrogen' is a little off too which may cause a failure if the user types it in correctly at the command line.
Related Question