MATLAB Object-Oriented Programming

(Joyce) #1
9 6 9 2
2 1 9 9

Add 7 to the array contained in the Data property:

obj + 7

ans =

15 16 10 16
16 13 16 9
9 8 16 16

MyDataClass.m


This definition for MyDataClass includes the end indexing method discussed in “end as
Object Index” on page 17-35.

classdef MyDataClass
% Example for "A Class with Modified Indexing"
properties
Data
Description
end
properties (SetAccess = private)
Date
end
methods
function obj = MyDataClass(data,desc)
% Support 0-2 args
if nargin > 0
obj.Data = data;
end
if nargin > 1
obj.Description = desc;
end
obj.Date = clock;
end

function sref = subsref(obj,s)
% obj(i) is equivalent to obj.Data(i)
switch s(1).type
case '.'
sref = builtin('subsref',obj,s);

17 Specialize Object Behavior

Free download pdf