Skip to content

Instantly share code, notes, and snippets.

@sg-s
Last active August 29, 2015 14:05
Show Gist options
  • Save sg-s/3935d57e4c297cf26746 to your computer and use it in GitHub Desktop.
Save sg-s/3935d57e4c297cf26746 to your computer and use it in GitHub Desktop.
Determine if this MATLAB function is being called by another function, and if so, what is it? The power of dbstack

How to find out if your MATLAB function is being called by another function or not

calling_func = dbstack;

if calling_func is non-empty, it is being called by some other function (as opposed to being called directly by the user from the command prompt)

How to find out the name of the function calling the current function

calling_func = dbstack;
if ~isempty(calling_func)
	calling_func_names = {calling_func.name};
end

The names are stored in reverse order of heirarchy.

How to find out if a certain function called the current function

calling_func = dbstack;
if ~isempty(calling_func)
	if find(strcmp('MyCoolFunc',{calling_func.name}))
		% your cool function is calling this function
	end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment