MATLAB Answers

How can I define a variable with a name based on an input?

127 views (last 30 days)
Nicole on 13 Aug 2018
Commented: Paolo on 14 Aug 2018
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 %#ok<NOPTS>
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 =
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.


Stephen Cobeldick
Stephen Cobeldick on 14 Aug 2018
"wanted to know if it was feasible to do this, regardless of how unnecessarily difficult it may be."
It is certainly feasible, as Paolo's answer shows you, but designing your code like this will force you into writing slow, complex, buggy code:
You would be much better off using the fields of a structure, or simple, efficient indexing with a cell array, or a table, or any of the other efficient ways to store data. For example, much more efficient would be a simple cell array:
C{end+1} = input('Name of the ship: ','s');
now you have a list of all of them in C. Having the data in one array makes it easy to process the whole list, making it simple to loop over them, or, for example, to find out if a particular ship is included:
Or you might use fields of a structure:
fld = input('Name of the ship: ','s');
S.(fld) = ... your data...
Possibly the best data format for your data would be a table: then you could easily store the ship name and other related data, and use all of the convenient tools that tables support for analyzing your data.
By designing your data better, you can make your code much simpler and more efficient. What you are doing will force you into writing slow, complex, buggy code. What you are doing means that you cannot use all of the MATLAB tools that operate on arrays/tables, like my simple example shows, because your data is not in one array but is spread around lots of separate variables. This is a really inefficient use of MATLAB.
You should change your data design.
Nicole on 14 Aug 2018
This is all true. Last night I finished the script I was working on, in the end using a 5-D cell array to store the ship data. Each ship has stages, each stage has engines, and each engine has characteristics, all of which must be input by the user, so this was both the simplest and the most robust way I could think to achieve this. Thanks for the info!
Paolo on 14 Aug 2018
Very good advice Stephen. Sometimes I just focus on answering the question without seeing the bigger picture, I guess that comes with experience :)

Sign in to comment.

Accepted Answer

Paolo on 13 Aug 2018
If I am understanding your question correctly, you can use:
ship_name = input('Name of the ship: ','s');
Will create a variable named such as Orange_name with value Orange.


Nicole on 13 Aug 2018
Thanks so much! That is exactly what I wanted. This place is so fast. The help page for the assignin function displays this:
--- help for assignin ---
assignin Assign variable in workspace.
assignin(WS,'name',V) assigns the variable 'name' in the
workspace WS the value V. WS can be one of 'caller' or 'base'.
What are 'caller' and 'base' and why do I want to use 'base' in this context?
Paolo on 13 Aug 2018
You are welcome, happy to help.
The usage of the base or caller options depends as to whether you are calling the assignin function from a script or a function (if called by another function).
For a script, use base.
For a function ( let's call it callee ), you use caller to assign the variable to the workspace of the function that called callee.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by