필터 지우기
필터 지우기

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

조회 수: 7 (최근 30일)
W. Feng
W. Feng 2020년 5월 25일
댓글: Stephen23 2020년 5월 27일
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
Walter Roberson
Walter Roberson 2020년 5월 27일
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 2020년 5월 27일
"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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 25일
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
Stephen23
Stephen23 2020년 5월 27일
편집: Stephen23 2020년 5월 27일
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(...)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by