How to read the name of all output argument in function?

8 views (last 30 days)
It is only known the name of M-file for the main function. AAA.m define as below
[¨P1,P2,P3,...P15]=function(a1,a2,a3,..a10)
...
I just want to read the name of output arguments, P1,P2,P3,...P15 is just assumption name we don't know.
We need to output it by some code. it could be stored with a cell array or other format, it doesn't matter.
how to accomplish it? thank you.
  3 Comments
Walter Roberson
Walter Roberson on 27 May 2020
No, there is no documented way to do that.
Perhaps you could take advantage of mtree() to examine the parse tree and match it against the location information returned by dbstack.
http://undocumentedmatlab.com/articles/function-definition-meta-info
Stephen23
Stephen23 on 27 May 2020
"I just want to read the name of output arguments, P1,P2,P3,...P15"
This implies that your variable names include some meta-data, e.g. case names, IDs, pseudo-indices, etc.
Putting meta-data into variable names should be avoided.
"Do you have any other solution for this?"
Don't put meta-data into variable names. The names of variables should not matter. Mixing data into your actual code fores you into writing slow, very fragile, highly obfuscated code that is liable to bugs and yet difficult to debug. Exactly like what you are trying to do now.
You would be much better of using arrays, tables, etc. to store your data.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 May 2020
In order to do this, you need to use dbstack('-completenames') to find out the name of the file that you were called from and the line number. Then you need to read that file as text, and work backwards to find the names of the outputs.
Remember that in the general case you might have to work backwards through several lines, and that you might have to deal with comments and continuation lines, and you might need to deal with indexed values that have no explicit names.
In the general case, you must also be prepared to examine the values of variables inside the calling function, so that you can figure out how to match up arguments.
For example,
[A, B{1:N}, C, D{1:M}] = function(a1,a2,a3,..a10)
then you would need to know the value of N in the calling function in order to know which of the outputs is named C
And in the general case you also need to deal with cases such as
[A(X,Y), B{F1(T):F2(Q)}, C, D{F3(R):F3(S)}] = function(a1,a2,a3,..a10)
so you might have to execute functions in the calling environment in order to figure out how output names line up. But you have the problem that executing those functions is not guaranteed to return the same value that they returned before, even given the same inputs!
My recommendation would be to give up on trying to do this.
  1 Comment
Stephen23
Stephen23 on 27 May 2020
Edited: Stephen23 on 27 May 2020
Also tricky:
str = '[any permitted syntax]'; % possibly defined in another workspace
...
eval([str,'=fun(...)'])
Or even just
C = cell(...); % possibly defined in another workspace
...
[C{:},D] = fun(...)

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!