How to print the caller command of a function?

26 views (last 30 days)
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?

Answers (2)

Steven Lord
Steven Lord on 7 Jul 2015
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 on 2 Jul 2015
Look at mfilename

Community Treasure Hunt

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

Start Hunting!