Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do I get the name of the caller function?

Subject: How do I get the name of the caller function?

From: Rob Campbell

Date: 29 Apr, 2010 14:34:04

Message: 1 of 6

Hi,

Say I write function "X" which I intend to be called by other Matlab functions. Is there a command I can insert into X which would report the name of the function that called X?

I don't want to have to pass the name of the caller function to X as an input variable. i.e. this would not be a suitable solution:
X(inArgA,inArgB,mfilename)

Cheers

Subject: How do I get the name of the caller function?

From: Malcolm Lidierth

Date: 29 Apr, 2010 14:43:04

Message: 2 of 6

Look at the dbstack function which gives you the entire calling sequence.
Regards
ML

Subject: How do I get the name of the caller function?

From: Rob Campbell

Date: 29 Apr, 2010 14:55:21

Message: 3 of 6

"Malcolm Lidierth" <ku.ca.lck@htreidil.mloclam> wrote in message <hrc5to$foq$1@fred.mathworks.com>...
> Look at the dbstack function which gives you the entire calling sequence.
> Regards
> ML

Nice one!
Thanks.

Subject: How do I get the name of the caller function?

From: Steven Lord

Date: 29 Apr, 2010 15:01:34

Message: 4 of 6


"Rob Campbell" <matlab@robertREMOVEcampbell.removethis.co.uk> wrote in
message news:hrc5cs$arl$1@fred.mathworks.com...
> Hi,
>
> Say I write function "X" which I intend to be called by other Matlab
> functions. Is there a command I can insert into X which would report the
> name of the function that called X?
> I don't want to have to pass the name of the caller function to X as an
> input variable. i.e. this would not be a suitable solution:
> X(inArgA,inArgB,mfilename)

How are you planning to use this functionality? If you're trying to throw
an error message "as though" it were coming from the caller function,
generate an MException and use its throwAsCaller method.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/throwascallermexception.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: How do I get the name of the caller function?

From: Rob Campbell

Date: 29 Apr, 2010 15:32:03

Message: 5 of 6

Steven, for the record what I was doing was

function varargout=X(varargin)
if nargout>2
disp("you're using function X with the old output format")
end

Being able to report who called function X is just a nicety so that people know where to look when updating code. dbstack does what I needed.

Subject: How do I get the name of the caller function?

From: Loren Shure

Date: 30 Apr, 2010 14:52:30

Message: 6 of 6

In article <hrc8pj$ti$1@fred.mathworks.com>,
matlab@robertREMOVEcampbell.removethis.co.uk says...
> Steven, for the record what I was doing was
>
> function varargout=X(varargin)
> if nargout>2
> disp("you're using function X with the old output format")
> end
>
> Being able to report who called function X is just a nicety so that people know where to look when updating code. dbstack does what I needed.
>

If you want this for the main function in the file, you can use
mfilename.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us