dbstack returns a structure containing information about the file, function name, and line number of the function call stack. The second element of the structure is the caller.
The structure will be empty if dbstack is called from the command line or by running the section in debug mode, and will contain only 1 element in the structure array if the function is called directly.
Example of stack info
stack = dbstack('-completenames')
2×1 struct array with fields:
struct with fields:
stack = dbstack('-completenames');
if numel(stack) >= 2
fprintf('Caller is %s line %d in file %s.\n', stack(2).name, stack(2).line, stack(2).file)
Note that the line number can be negative when stepping past the end of the file in debug mode.