How to print the caller command of a function?
26 views (last 30 days)
Show older comments
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 Comments
Answers (2)
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.
0 Comments
See Also
Categories
Find more on String 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!