Main Content

coder.mfunctionname

Name of calling function or method

Description

example

name = coder.mfunctionname returns the name of the function or method in whose body this function call is placed. For anonymous functions, name contains the function definition.

When debugging your MATLAB® code or the generated code, use this function to access the name of the currently running function or method.

Examples

collapse all

Use coder.mfunctionname to access the name of the currently running function.

Define the MATLAB function sumOfDeviations:

function y = sumOfDeviations(x)
y = sum(x - mean(x),'all');
fprintf('%s returned the value: %f\n',coder.mfunctionname,y);
end

Call sumOfDeviations with a 4-by-4 input:

sumOfDeviations(magic(4));
sumOfDeviations returned the value: 0.000000

Generate a MEX function for the sumOfDeviations function. Specify the input as a 4-by-4 double.

codegen sumOfDeviations -args {zeros(4)}
Code generation successful.

Call the generated MEX function sumOfDeviations_mex with the same 4-by-4 input:

sumOfDeviations_mex(magic(4));
sumOfDeviations returned the value: 0.000000

Use coder.mfunctionname to access the text of the currently running anonymous function.

Define the MATLAB function foo that both defines and calls an anonymous function. The anonymous function calls coder.mfunctionname inside its body.

function foo
z = @(~) fprintf('Currently running: %s\n',coder.mfunctionname);
z();
end

Call foo at the MATLAB command line:

foo
Currently running: @(~)fprintf('Currently running: %s\n',coder.mfunctionname)

Generate a MEX function for foo.

codegen foo
Code generation successful.

Call the generated MEX function.

foo_mex
Currently running: foo/@(~)fprintf('Currently running: %s\n',coder.mfunctionname)

Output Arguments

collapse all

Name of the function or method that called the coder.mfunctionname function, returned as a character vector.

In certain special cases, the output of coder.mfunctionname in the generated code might be different from MATLAB execution:

Context of calling coder.mfunctionnameOutput in MATLABOutput in Generated Code

Inside a method placed in a class folder (@-folder)

For example, MyMethod of MyClass.

'MyMethod'

'MyClass.MyMethod'

Inside a class constructor.

For example, constructor of MyClass.

'MyClass.MyClass'

'MyClass (constructor)'

Inside an anonymous function.

Text of the anonymous function.

Concatenation of these two character vectors, separated by a forward slash character:

  • Output of coder.mfunctioname for the function enclosing the anonymous function

  • Text of the anonymous function

See Access Text of Anonymous Function.

Extended Capabilities

See Also

Introduced in R2021b