MATLAB Object-Oriented Programming

(Joyce) #1
% Implement obj(indices) = varargin{:};
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj(indices).PropertyName = varargin{:};
...
elseif length(s) == 3 && strcmp(s(2).type,'.') && strcmp(s(3).type,'()')
% Implement obj(indices).PropertyName(indices) = varargin{:};
...
else
% Use built-in for any other expression
obj = builtin('subsasgn',obj,s,varargin{:});
end
case '{}'
if length(s) == 1
% Implement obj{indices} = varargin{:}
...
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj{indices}.PropertyName = varargin{:}
...
% Use built-in for any other expression
obj = builtin('subsasgn',obj,s,varargin{:});
end
otherwise
error('Not a valid indexing expression')
end
end

Using varargin for the right-side value of the assignment statement enables the method
to work with object arrays. For example, suppose that you want to support the assignment
of a comma-separated list with an expression like this one:

C = {'one';'two';'three'};
[objArray.PropertyName] = C{:}

This expression results in an indexing structure with the dot type ('.') indexing The cell
array C on the right side of the assignment statement produces a comma-separated list.
This code assigns one list item to each property in the object array.

case '.'
if length(s)==1
prop = s(1).subs; % Property name
n = numel(obj); % Number of elements in array
for k = 1:n
obj(k).(prop) = varargin{k};
end
end
end

17 Specialize Object Behavior

Free download pdf