Setter methods for dynamic properties

8 ビュー (過去 30 日間)
Andrew Barton
Andrew Barton 2012 年 9 月 18 日
回答済み: Sebastian Hölz 2025 年 6 月 18 日
Hello,
I have been working with a class that has dynamic properties that I would like to have setter methods for. The problem is that I don't know how many or the names of the dynamic properties, so I want to have just one setter that takes the name of the property as an argument. However this breaks the part of MATLAB that determines whether or not you're in a setter function (so that when you set the variable it just sets the variable, instead of calling the setter recursively). Here's some example code that illustrates what I'm talking about:
classdef myClass < dynamicprops
methods
function obj = myClass()
P = addprop(obj, 'myProp');
P.SetMethod = @(o,p)SetProp(o,'myProp',p);
end
function SetProp(obj, propname, val)
obj.(propname) = val;
end
end
end
Now if you try:
x = myClass();
x.myProp = 7;
the method SetProp gets called recursively until MATLAB throws an error. Is there another way to go about this that I am missing? Thanks in advance.
-Andrew
  3 件のコメント
Andrew Barton
Andrew Barton 2012 年 9 月 19 日
Not quite, I'd really like the user of the class to be able to assign the property using normal syntax, i.e:
x.someprop = value

サインインしてコメントする。

採用された回答

Daniel Shub
Daniel Shub 2012 年 9 月 19 日
What about overloading subsasgn
classdef myClass < dynamicprops
methods
function obj = myClass()
P = addprop(obj, 'myProp');
end
function A = subsasgn(A, S, B)
switch S.type
case '.'
SetProp(A, S.subs, B);
end
end
function SetProp(obj, propname, val)
obj.(propname) = val;
end
end
end
  2 件のコメント
Andrew Barton
Andrew Barton 2012 年 9 月 19 日
Thanks for the idea, that looks like it would fix my problem, but it also seems like it would force me to reimplement normal array indexing for S.type case '()' which I would obviously rather avoid.
I really think there must be a way to pass the name of the property assigned to the SetMethod function, otherwise attaching SetMethods to dynamic properties would be completely useless in all cases.
Andrew Barton
Andrew Barton 2012 年 9 月 20 日
Nevermind, I just now stumbled on the 'builtin' funtion. This solution works perfectly.

サインインしてコメントする。

その他の回答 (1 件)

Sebastian Hölz
Sebastian Hölz 2025 年 6 月 18 日
I believe the best method is to temporarily disable the property set method in the defined setter-method:
classdef myClass < dynamicprops
methods
function obj = myClass()
prop_name = 'myProp'
P = addprop(obj, prop_name);
prop.SetMethod = @(obj, val)SetProp(obj, prop_name, val, P);
end
function SetProp(obj, prop_name, val, P)
% disable setter-method
tmp = P.SetMethod;
P.SetMethod = [];
% Set value, this could include some more error checking ...
obj.(prop_name) = val;
% Reset setter-method
P.SetMethod = tmp;
end
end
end

カテゴリ

Help Center および File ExchangeFunctions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by