>>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 endend
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