How can I get a variable name from a cell?

10 visualizzazioni (ultimi 30 giorni)
Brayden Gregson
Brayden Gregson il 1 Ott 2015
Modificato: James Tursa il 1 Ott 2015
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?

Risposte (2)

James Tursa
James Tursa il 1 Ott 2015
Modificato: James Tursa il 1 Ott 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 il 1 Ott 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.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by