How to print the caller command of a function?

조회 수: 10 (최근 30일)
Mr M.
Mr M. 2015년 7월 1일
답변: Steven Lord 2015년 7월 7일
Inside a function, I want to print the command, which is used to call that function. For example print the following string: 'This data is generated by the command myfunc(2,'abc',100);' What is the simplest way to do this? is it any general solution?

답변 (2개)

Steven Lord
Steven Lord 2015년 7월 7일
You can get the name of the calling function using MFILENAME or DBSTACK. The larger problem is to get the input arguments. You can, in some LIMITED circumstances, use INPUTNAME; however, this will not work for all situations (if the caller uses an expression like "x+1" or a literal number "100" as the input rather than a variable name.)
There is no way, short of reading in the calling function and parsing its code as a string or requiring callers of your function to pass in strings representing the input arguments or the like, to do what you're asking. But if you're trying to add that code as a diagnostic message to help you debug a problem, a much better option (in my opinion) is to use the debugging tools included in MATLAB.

Walter Roberson
Walter Roberson 2015년 7월 2일
Look at mfilename
  댓글 수: 1
Mr M.
Mr M. 2015년 7월 7일
my problem is not to get the name of the script or function

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by