MATLAB Object-Oriented Programming

(Joyce) #1
Create an instance of the class:

obj = MyDataClass(d,'Test001')

obj =

MyDataClass with properties:

Data: [3x4 double]
Description: 'Test001'
Date: [2012 1 7 9 32 34.5190]

The constructor arguments pass the values for the Data and Description properties.
The clock function assigns the value to the Date property from within the constructor.
This approach captures the time and date information when each instance is created.

Here is the preliminary code listing without the subsref, subsasgn double, and plus
methods.

classdef MyDataClass
properties
Data
Description
end
properties (SetAccess = private)
Date
end
methods
function obj = MyDataClass(data,desc)
if nargin > 0
obj.Data = data;
end
if nargin > 1
obj.Description = desc;
end
obj.Date = clock;
end
end
end

Specialize Subscripted Reference — subsref


Implement a subsref method to support both the default and a specialized type of
indexing.

17 Specialize Object Behavior

Free download pdf