In matlab one can use dbstack to retrieve the call stack at the current time, however dbstack is not available in standalone compiled versions of matlab programs, is there an alternative to get the call stack, or at least the function calling the current function? I want to write a facility function that needs to know by who it was called, but a full call stack would be preferable.
Here's where the solutions stand so far:
However, I have one more possible solution that I think is the "cleanest" one yet: using the error handling mechanisms to get at the stack trace. This will vary based on the MATLAB version you are using...
New error-handling capabilities in the form of the MException class were introduced in Version 7.5. You can get information about the stack trace from MException objects by creating and throwing a "dummy" exception, then immediately catching it and accessing the stack
field. If you do the following in a function:
try
throw(MException('phony:error',''));
catch ME
callerStack = {ME.stack.name};
end
Then the cell array callerStack
will contain the names of all the functions in the call stack, with the current function name in the first element and the top-most caller name in the last element.
For these earlier versions you can use the ERROR function to throw an error and the LASTERROR function to capture the error and get the stack information:
try
error('phony:error','');
catch
s = lasterror;
callerStack = {s.stack.name};
end
Unfortunately, the LASTERROR function only started returning stack trace information in MATLAB Version 7.1, so there is no version of the above solutions that I can come up with for earlier MATLAB versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With