How can I get a variable name from a cell?

5 views (last 30 days)
How can I get a variable name from a cell? For example, I have:
alpha_1 = 1;
galaxyMap = {alpha_1};
How do I get the name alpha_1 and print it to the command window?

Answers (2)

James Tursa
James Tursa on 1 Oct 2015
Edited: James Tursa on 1 Oct 2015
Once the variable has been put into a cell, you can't recover the original variable name from the cell. If you need that information you would have to save it separately somewhere (e.g., maybe in another cell array). Why do you need this?
EDIT:
Actually, there is a way if the original variable is still in the workspace and neither variable (the original or the cell copy) has changed and they are only shared with each other and nothing else (that's a lot of if's). But it involves a mex routine to look at the data pointers to see which workspace variable is shared with the cell element variable. Probably not the solution you wanted.

Star Strider
Star Strider on 1 Oct 2015
If your workspace includes the variable, you can recover it using the who function, and print it to the Command Window:
alpha_1 = 1;
galaxyMap = {alpha_1};
list_variables = who('al*');
variable_names = list_variables{:}
variable_names =
alpha_1
See the documentation on who (and whos) for details.
This is not easy or straightforward, especially if you have several variable names that are similar.

Categories

Find more on Tables 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!