MATLAB Object-Oriented Programming

(Joyce) #1
statement implements all indexing expressions that begin with that first-level type of
indexing.

The methods must implement all indexing expressions that the class supports. If you do
not customize a particular type of indexing, call the built-in function to handle that
expression.

Use the length of the indexing structure array and indexing type define conditional
statements for compound indexing expressions.

Code Framework for subsref Method

The following framework for the subsref method shows how to use information in the
indexing structure in conditional statements. Your application can involve other
expression not shown here.
function varargout = subsref(obj,s)
switch s(1).type
case '.'
if length(s) == 1
% Implement obj.PropertyName
...
elseif length(s) == 2 && strcmp(s(2).type,'()')
% Implement obj.PropertyName(indices)
...
else
[varargout{1:nargout}] = builtin('subsref',obj,s);
end
case '()'
if length(s) == 1
% Implement obj(indices)
...
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj(ind).PropertyName
...
elseif length(s) == 3 && strcmp(s(2).type,'.') && strcmp(s(3).type,'()')
% Implement obj(indices).PropertyName(indices)
...
else
% Use built-in for any other expression
[varargout{1:nargout}] = builtin('subsref',obj,s);
end
case '{}'
if length(s) == 1
% Implement obj{indices}
...
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj{indices}.PropertyName
...
else
% Use built-in for any other expression

17 Specialize Object Behavior

Free download pdf