How to print the caller command of a function?

10 visualizaciones (últimos 30 días)
Mr M.
Mr M. el 1 de Jul. de 2015
Respondida: Steven Lord el 7 de Jul. de 2015
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?

Respuestas (2)

Steven Lord
Steven Lord el 7 de Jul. de 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 el 2 de Jul. de 2015
Look at mfilename
  1 comentario
Mr M.
Mr M. el 7 de Jul. de 2015
my problem is not to get the name of the script or function

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by