How to print the caller command of a function?
조회 수: 10 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!