How can I get a variable name from a cell?
조회 수: 2 (최근 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?
댓글 수: 0
답변 (2개)
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.
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Filename Construction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!