主要内容

metafunction

Create metadata object that describes function or method

Since R2026a

    Description

    obj = metafunction(identifier) returns a matlab.metadata.Function or matlab.metadata.Method object that corresponds to the identifier.

    • Functions and class constructors— The name of the function or constructor. For example, obj = metafunction("myFunction").

    • Class methods — The name of the defining class and the name of the method separated by /. For example, obj = metafunction("MyClass/myMethod").

    • Local functions — The name of the main function file and the name of the local function separated by >. For example, obj = metafunction("myFunction>myLocalFunction").

    example

    obj = metafunction(identifier,Arguments={val1,...,valN}) specifies a cell array of argument values for the function or method. obj describes the function or method that MATLAB® would call given the same input argument values.

    example

    obj = metafunction(identifier,ArgumentTypes=["type1",...,"typeN"]) specifies a string array containing one or more argument types for the function or method. obj describes the function or method that MATLAB would call given the same input argument types.

    example

    Examples

    collapse all

    Save the function file centerLineOnIntercept.m on your path.

    function centerLineOnIntercept(x,y)
    % centerLineOnIntercept Plot a line, center on y-intercept 
    % Plot a line based on two points on a 10-by-10 graph. Center the 
    % graph on the y-intercept of line.
    
    arguments (Input)
        x (1,2) {mustBeNumeric}
        y (1,2) {mustBeNumeric}
    end
        xval = linspace(-5,5);
        [m,b] = calcSlopeIntercept(x,y);
        yval = m*xval + b;
        plot(xval,yval)
        xline(0)
        yline(0)
        xlim([-5 5])
        ylim([b-5 b+5])
    end
    
    function [m,b] = calcSlopeIntercept(x,y)
        m = (y(2) - y(1))/(x(2) - x(1));
        b = y(1) - m*x(1);
    end

    To get information about the function using introspection, call metafunction to create a matlab.metadata.Function instance.

    mf = metafunction("centerLineOnIntercept")
    mf = 
    
      Function with properties:
    
                       Name: 'centerLineOnIntercept'
                Description: 'Plot a line, center on y-intercept '
        DetailedDescription: '  Plot a line based on two points on a 10-by-10 graph. Center the 
                                graph on the y-intercept of line.'
                   FullPath: 'C:\centerLineOnIntercept.m'
              NamespaceName: ''
                  Signature: [1×1 matlab.metadata.CallSignature]

    To get information about calcSlopeIntercept using introspection, call metafunction to create a matlab.metadata.Function instance for the local function. The FullPath property shows that the centerLineOnIntercept.m file contains the calcSlopeIntercept function.

    mf = metafunction("centerLineOnIntercept>calcSlopeIntercept")
    mf = 
    
      Function with properties:
    
                       Name: 'calcSlopeIntercept'
                Description: ''
        DetailedDescription: ''
                   FullPath: 'C:\centerLineOnIntercept.m'
              NamespaceName: ''
                  Signature: [1×1 matlab.metadata.CallSignature]
    

    For more details on the information you can get from the Signature property of a Function instance, see matlab.metadata.CallSignature.

    Save the class Rectangle on your path.

    classdef Rectangle
        properties
            X (1,1) double {mustBeReal} = 0
            Y (1,1) double {mustBeReal} = 0
            Width (1,1) double {mustBeReal} = 0
            Height (1,1) double {mustBeReal} = 0
        end
        
        methods
            function R = enlarge(R,x,y)
                arguments (Input)
                    R (1,1) Rectangle
                    x (1,1) {mustBeNonnegative}
                    y (1,1) {mustBeNonnegative}
                end
                arguments (Output)
                    R (1,1) Rectangle
                end
                R.Width = R.Width + x;
                R.Height = R.Height + y;
            end
        end
    end

    To get information about the enlarge method using introspection, call metafunction to create a matlab.metadata.Method instance.

    mf = metafunction("Rectangle/enlarge")
    mf = 
    
      Method with properties:
    
                       Name: 'enlarge'
                Description: ''
        DetailedDescription: ''
                     Access: 'public'
                     Static: 0
                   Abstract: 0
                     Sealed: 0
                     Hidden: 0
                  Signature: [1×1 matlab.metadata.CallSignature]
                   FullPath: 'C:\Rectangle.m'
              DefiningClass: [1×1 matlab.metadata.Class]

    For more details on the information you can get from the Signature property of a Method instance, see matlab.metadata.CallSignature.

    You can call metafunction with Arguments to identify what method MATLAB calls with a specific set of argument values.

    The DocPolynom class provides a representation of polynomials, including the ability to perform basic operations on them. Add the definition of DocPolynom to your path and create an instance. (See Representing Polynomials with Classes for the full code for the class.)

    pObj = DocPolynom([1 1 3])
    pObj = 
    
    x^2 + x + 3

    DocPolynom overloads the plus operator to support addition of DocPolynom instances as well as a DocPolynom and a numeric value. For example, adding a 5 of type double to pObj adds 5 to the last coefficient of pObj.

    pObj = plus(pObj,5)
    pObj = 
    
    x^2 + x + 8

    To identify the method called for this operation, call metafunction with the method name and the argument values. The DocPolynom instance is the dominant argument because DocPolynom is a user-defined class. Access the DefiningClass property of the resulting matlab.metadata.Method instance to show that the version of plus that MATLAB calls is the overload defined by DocPolynom.

    mp = metafunction("plus",Arguments={pObj,5});
    mp.DefiningClass.Name
    ans =
    
        'DocPolynom'

    You can use a similar process by calling metafunction with argument types instead of specific argument values.

    mp = metafunction("plus",ArgumentTypes=["DocPolynom","double"]);
    mp.DefiningClass.Name
    ans =
    
        'DocPolynom'

    Input Arguments

    collapse all

    Identifier of a function or method, specified as a string scalar or character vector. The format of the text depends on the type of function or method.

    • Functions and class constructors — The name of the function or constructor. For example, obj = metafunction("myFunction").

    • Methods — The name of the defining class and the name of the method separated by /. For example, obj = metafunction("MyClass/myMethod"). This syntax works for non-static and static methods, but as an alternative for static methods, you can use a dot instead of /. For example, obj = metafunction("MyClass.myStaticMethod").

    • Local functions — The name of the main function file and the name of the local function separated by >. For example, obj = metafunction("myFunction>myLocalFunction").

    Namespace functions and classes in these syntaxes must be fully qualified. For example obj = metafunction("myNamespace.myNamespaceFunction") and obj = metafunction("myOuterspace.myInnernamespace.MyClass/myMethod").

    Nested functions are not supported.

    Arguments for the function or method, specified as a cell array. Specify values of the type and size that the function or method accepts.

    Specifying an empty cell array is the same as calling the function without this argument.

    Example: mf = metafunction("plus",Arguments={5,"word"})

    Argument types for the function or method, specified as a string array of class names. You can specify a starting subset of argument types, but for the most accurate information, specify the types of all the arguments the function or method accepts.

    Specifying an empty string array is the same as calling the function without this argument.

    Example: mf = metafunction("plus",ArgumentTypes=["double","string"])

    Output Arguments

    collapse all

    Function or method metadata, specified as a matlab.metadata.Function or matlab.metadata.Method instance, respectively.

    Version History

    Introduced in R2026a