builtin - Execute built-in function from overloaded method - MATLAB (original) (raw)

Execute built-in function from overloaded method

Syntax

Description

builtin([func](#bthubnb-function),[x1,...,xn](#bthubnb-x1xn)) executes the built-in function func with the input argumentsx1 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](#bthubnb-function),[x1,...,xn](#bthubnb-x1xn)) stores any output from function in y1 throughyn.

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. Overloaddisp 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 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. Callingdisp 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

Extended Capabilities

Version History

Introduced before R2006a