How to know the name of the function or object passed.

2 views (last 30 days)
Hi ,
I have a requirement where in the user provides me with a object as input and i have to print the name of the object provided into a file.
simple example
Input
X = { magic(3) , coder.config('Mex') }
Output
<setup>config = coder.config('Mex')
input = magic(3)
. Please is there any solution by which i can revieve the name of the input class or function instead of the values it has . Presently i am getting a matrix which is a magic matrix and the content of the object.

Answers (1)

Walter Roberson
Walter Roberson on 12 Apr 2019
In MATLAB, the only way to do that is to use dbstack('-completenames') to find out which file and line your caller is, and then you have to read the file and find that line, and try to figure out from the text of the line which object was passed to you.
This will not work if you were invoked from the command line. If you were invoked within an eval() or evalin() then you might have problems, perhaps.
In the general case, the same method could be called multipel times on the same line, such as:
X1 = { magic(3), coder.config('Mex') }; X2 = { latinsquare(6), coder.config('Mex') };
In order to track which of the two is the active one, you would need to keep a history of who called you recently -- the first time on any given line is the left-most occurance, the second time is right of that, and so on.
There is no other way to do this in MATLAB.
If you were passing a variable name, like,
magic3 = magic(3);
ccm = coder.config('Mex')
do_setup( magic3, ccm )
instead of an expression, then you could use argname() to find the name of the variable that was passed. argname() returns empty for expressions of any sort, so you cannot use it to grab expression such as magic(3)

Products

Community Treasure Hunt

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

Start Hunting!