How can I get a variable name from a cell?

5 次查看(过去 30 天)
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?

回答(2 个)

James Tursa
James Tursa 2015-10-1
编辑:James Tursa 2015-10-1
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 2015-10-1
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.

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by