Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab get string containing variable name [duplicate]

Tags:

matlab

In Matlab, how can i get a String containing "GRUMPY" given the following declaration:

  GRUMPY = 500;

This is usually called reflection in other programming languages, but i cannot find an example of it in Matlab.

like image 561
NWS Avatar asked Jul 12 '12 13:07

NWS


2 Answers

MATLAB doesn't provide built-in functionality for this, but there is a workaround, as employed here

Essentially, you have to create your own function to do this. Take advantage of Matlab's functionality for getting the variable name of the INPUT ARGUMENT to a function.

I.e.

function out = varname(var)   out = inputname(1); end 

Then

GRUMPY = 500; name = varname(GRUMPY) 

will give you what you want.

like image 98
Patrick Avatar answered Oct 24 '22 22:10

Patrick


If I understand correctly you should try

who GRUMPY 

or

which GRUMPY 
like image 25
Hasan Avatar answered Oct 24 '22 22:10

Hasan