MATLAB Object-Oriented Programming

(Joyce) #1

  • Perform the necessary steps in the method to implement the function. For example,
    access the object properties to manipulate data.


Generally, the method that overloads a function produces results similar to the MATLAB
function. However, there are no requirements regarding how you implement the
overloading method. The overloading method does not need to match the signature of the
overloaded function.


NoteMATLAB does not support overloading functions using different signatures for the
same function name.


Overload the bar Function


It is convenient to overload commonly used functions to work with objects of your class.
For example, suppose that a class defines a property that stores data that you often
graph. The MyData class overrides the bar function and adds a title to the graph:


classdef MyData
properties
Data
end
methods
function obj = MyData(d)
if nargin > 0
obj.Data = d;
end
end
function bar(obj)
y = obj.Data;
bar(y,'EdgeColor','r');
title('My Data Graph')
end
end
end


The MyData bar method has the same name as the MATLAB bar function. However, the
MyData bar method requires a MyData object as input. Because the method is
specialized for MyData objects, it can extract the data from the Data property and create
a specialized graph.


To use the bar method, create an object:


Overload Functions in Class Definitions
Free download pdf