MATLAB: Implementing an hgsetget subclass

classhsetgetmatlab oopset

I would like to create a subclass of hgsetget that would show this kind of behavior:
>>u=unit('SI');
>>set(u) % set behavior 1
System: '[{SI} | cgs]'
>>set(u,'System') % set behavior 2
[{SI} | cgs]
>>set(u,'System','cgs') % set behavior 3
>>get(u,'System')
ans =
cgs
In other words, much like the behavior you see for a figure handle. Following the example in Implementing a Set/Get Interface for Properties, I tried this code:
classdef unit < hgsetget
properties
System = '';
end
methods
function H = unit(str)
if nargin > 0
H.System = str;
end
end
function H = set.System(H,str)
H.System = str;
end
end
end
This gets set behavior 3 right, but how do I add the other two? The closest I have come so far is to simply program all the cases into an overloaded set method, and get rid of set.System. But if I add a few more properties, this gets quite ugly. Does anyone know an elegant way of doing this?

Best Answer

O.k., this works:
classdef unit < hgsetget
% This is just a test class for using SET in an HGSETGET subclass.
properties
System = '';
end
methods
function H = unit(str)
if nargin > 0
H.System = str;
end
end
function varargout = set(H,varargin)
S = struct('System','[{SI} | cgs]');
switch nargin
case 1
if nargout < 1
disp(S)
else
varargout{1} = S;
end
case 2
if nargout < 1
disp(S.System)
else
varargout{1} = S.System;
end
case 3
H.System = varargin{2};
otherwise
error('unit:wrongNumberOfArguments', ...
'Wrong number of arguments to SET command.')
end% switch
end% set
end% methods
end% classdef
But I still wonder - is there an implementation that involves a function SET.SYSTEM?