I want my script to create new variables whose names depend on an input, e.g.:
ship_name = input('Name of the ship: ','s');
[the string, ship_name, here]_name = ship_name
I know I want the latter half of this second variable to be '_name', but how do I get the former half to be identical to the string input by the user, so this happens in the command window:
Name of the ship: Orange
Orange_name =
"Orange"
Over time, many different ships exist, such that there may be an Orange_name, a Green_name, and a Blue_name. My script will later know which ship it's working with:
ship_current = input('Which ship are you working with? ');
And I want it to return the string called Orange_name when ship_current = "Orange", the string called Green when ship_current = "Green", and so forth. This may seem useless, but there will also be ship_mass and ship_capacity variables, so the script will basically remember the details of each ship until they're needed later. I also think I can achieve the same thing by concatenating onto a cell containing this information, but I had this variable name idea and wanted to know if it was feasible to do this, regardless of how unnecessarily difficult it may be.
3 Comments
Stephen Cobeldick (view profile)
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/414710-how-can-i-define-a-variable-with-a-name-based-on-an-input#comment_599509
Nicole (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414710-how-can-i-define-a-variable-with-a-name-based-on-an-input#comment_599768
Paolo (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/414710-how-can-i-define-a-variable-with-a-name-based-on-an-input#comment_599885
Sign in to comment.