Main Content

builtin

Execute built-in function from overloaded method

Description

builtin(func,x1,...,xn) executes the built-in function func with the input arguments x1 through xn. A built-in function is part of the MATLAB® executable.

When you define a method that overloads a built-in function for a class, you can use builtin to execute the original built-in function from within the overload.

example

[y1,...,yn] = builtin(func,x1,...,xn) stores any output from function in y1 through yn.

Examples

collapse all

Define a class that overloads the built-in function disp but also uses the built-in functionality from within the overloaded method.

Define the MyParticle class to describe the velocity of a particle in a structure with fields for the x, y, and z directions. Overload disp for the class so that the default display for scalar instances of MyParticle also lists the values stored in the velocity property.

classdef MyParticle
    properties
        velocity
    end
    methods
        function p = MyParticle(x,y,z)
            p.velocity.x = x;
            p.velocity.y = y;
            p.velocity.z = z;
        end
        function disp(p)
            builtin("disp",p)
            if isscalar(p)
                disp('  velocity')
                disp(['    x: ',num2str(p.velocity.x)])
                disp(['    y: ',num2str(p.velocity.y)])
                disp(['    z: ',num2str(p.velocity.z)])
            end
        end
    end
end

Create a scalar instance of MyParticle. The call to the built-in disp provides the standard class display, including class name and property information. Because the instance is scalar, the overloaded disp method also displays the values of the structure in velocity.

a = MyParticle(1,2,3)
a = 

  MyParticle with properties:

    velocity: [1×1 struct]

  velocity
    x: 1
    y: 2
    z: 3

Using builtin to call disp gives access to the default disp behavior. Calling disp on an instance of MyParticle inside the overloaded method without using builtin causes an infinite loop. The calls to disp inside the if statement do not cause infinite loops because the inputs are character vectors, not instances of MyParticle.

Input Arguments

collapse all

Built-in function name, specified as a character vector or string scalar. func cannot be a function handle.

Valid input arguments for func, specified as one or more values of supported data types for func.

Tips

  • You can use the syntax which function to check whether a function is built-in.

  • builtin does not work properly if overloaded.

  • The example shows a simple way of customizing the default display for a class. For more display customization options, see Custom Display Interface.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a