How can I get a variable name from a cell?

6 ビュー (過去 30 日間)
Brayden Gregson
Brayden Gregson 2015 年 10 月 1 日
編集済み: James Tursa 2015 年 10 月 1 日
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 Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by