Objects in OpenMatrix Language
Objects in OML are defined using the classdef keyword. This creates a blueprint for an object but does not actually create (instantiate) anything.
- The declaration including the name and parent class.
- The properties section.
- The methods section.
classdef MyClass
end
classdef MyClass
properties
name
value
end
end
m = MyClass;
m.name = ‘fun’;
m.value = 0;
classdef MyClass
properties
name = ‘fun’;
value = 0;
end
end
m = MyClass;
classdef MyClass
properties
name
value
end
methods
function obj = AddOne(self)
obj.value = self.value+1;
obj.name = self.name;
end
end
end
m = MyClass;
m.name = ‘fun’;
m.value = 0;
m = m.AddOne();
classdef MyClass
properties
name
value
end
methods
function obj = MyClass(in_name, in_value)
obj.name = in_name;
obj.value = in_value;
end
end
end
m = MyClass(‘fun’, 3);
classdef Employee
properties
name;
ID;
end
methods
function obj = Employee (in_name, in_ID)
obj.name = in_name;
obj.ID = in_ID;
end
end
end
classdef Supervisor < Employee
properties
group
end
methods
function obj = Supervisor(in_name, in_ID)
obj.name = in_name;
obj.id = in_ID;
obj.group = {};
end
end
end
super = Supervisor(‘Chris’, 4)
function obj = Supervisor(in_name, in_ID)
obj = obj@Employee(in_name, in_ID);
obj.group = {};
end
classdef MyClass
properties
name
value
end
methods
function obj = AddOne(self)
obj.value = self.value+1;
obj.name = self.name;
end
end
end
m = MyClass;
m.name = ‘fun’;
m.value = 0;
m = m.AddOne()
classdef MyClass < handle
properties
name
value
end
methods
function AddOne(self)
self.value = self.value+1;
end
end
end
m = MyClass;
m.name = 'fun';
m.value = 0;
m.AddOne();
m
classdef ratnum
properties
n
d
end
methods
function r = ratnum(numerator, denomenator)
r.n = numerator;
r.d = denomenator;
end
function r = sqrt(r)
r = sqrt(r.n/r.d);
end
end
end
a = ratnum(2,3);
sqrt(a)
In this case, when sqrt is called, the class’s method is used instead of the standard built-in function (which is unaware of user-defined classes).
classdef ratnum
properties
n
d
end
methods
function r = ratnum(numerator, denomenator)
r.n = numerator;
r.d = denomenator;
end
function disp(r)
if (r.d ~= 1)
fprintf('%d/%d\n', r.n, r.d);
else
fprintf('%d\n', r.n);
end
end
end
end
a = ratnum(2,3)
classdef ratnum
properties
n
d
end
methods
function r = ratnum(numerator, denomenator)
r.n = numerator;
r.d = denomenator;
end
function disp(r)
if (r.d ~= 1)
fprintf('%d/%d\n', r.n, r.d);
else
fprintf('%d\n', r.n);
end
end
function plus(r1, r2)
if (class(r2) == 'ratnum')
r = ratnum(r1.n*r2.d + r2.n*r1.d, r1.d * r2.d);
elseif (isscalar(r2))
r = ratnum(r1.d*r2 + r1.n, r1.d);
else
r = 0;
end
end
end
end
ratnum(1,3)+ratnum(1,2)
The first parameter to any overloaded method or operator is guaranteed to be of the specified type, but any additional arguments need to be checked and handled by the class developer. In the previous example, r1 is always a ratnum, but r2 can be a ratnum, a scalar, or anything else (for example, a cell array).
- +
- plus
- -
- minus
- *
- mtimes
- .*
- times
- /
- mrdivide
- .
- rdivide
- \
- mldivide
- .\
- ldivide
- ==
- eq
- ~=
- ne
- ^
- mpower
- .^
- power
- uminus
- unary minus negates a single object instead of subtracting two objects
classdef MyClass
properties (Access = private)
name
value
end
methods
function obj = MyClass(in_name, in_value)
obj.name = in_name;
obj.value = in_value;
end
end
end
m = MyClass(‘Ted’, 4)
m.name = ‘Bob’ % this triggers a run-time error