Using builtin assignment for classes that overload the assignment operator '='

Hi,
I'm trying to find out if it is possible to recover the functionality of '=' for properties in a class that have their own set method.
For instance, this piece of code:
classdef myClass < hgsetget
properties
myProperty
end
methods
function set.myProperty(obj,value)
mySetFunction(obj,value);
end
function mySetFunction(obj,value)
obj.myProperty = value;
end
end
end
generates a recursion error if we try to set the property myProperty by any value.
Is there a way to avoid this effect? If there were a builtin function that represents the effect of the "=" I could use it inside mySetFunction, but I do not know if this is possible.
In other words, I know that inside the set.myProperty function, a command
obj.myProperty = value;
would call the builtin function corresponding to "=", but I do not know how to force this behaviour explicitely in any function that is not a set.<property>
Best, Daniel

 채택된 답변

You are correct, your code does throw an error about recursion limits. However, I believe the operator you are interested in overloading is the subscripted assignment operator, "subsasgn". This will allow you to do:
obj.myProperty = value;
and call you own set function without receiving an error about the recursion limit. Here is what the class definition would look like:
classdef myClass < hgsetget
properties
myProperty
end
methods
function mySetFunction(obj,value)
obj.myProperty = value;
end
function a = subsasgn(a,s,b)
if strcmp(s.subs,'myProperty')
mySetFunction(a,b);
end
end
end
end

댓글 수: 3

Hi Philip,
Many thanks. This comes very close to what I need. My further question would be: with the definition that suggest is there any way that I can write
g = myClass();
g.myProperty = value;
without activating mySetFunction?
The reason of my question is this: imagine mySetFunction had a further functionalirty, say:
classdef myClass < hgsetget
properties
myProperty
end
methods
function mySetFunction(obj,value)
disp('myProperty has been updated');
obj.myProperty = value;
end
function a = subsasgn(a,s,b)
if strcmp(s.subs,'myProperty')
mySetFunction(a,b);
end
end
end
end
Then my final goal is a class where the direct assignation of a value triggers the "myProperty updated" message, BUT in which I keep the ability of updating myProperty programmatically without triggering the message.
Do you think that is possible? Daniel
The only way I can think of achieving this is by adding an additional property to the class:
classdef myClass < hgsetget
properties
myProperty
printMessage
end
methods
function mySetFunction(obj,value)
if (obj.printMessage)
disp('myProperty has been updated');
end
obj.myProperty = value;
end
function a = subsasgn(a,s,b)
if strcmp(s.subs,'myProperty')
mySetFunction(a,b);
else
a.(s.subs) = b;
end
end
end
end
The message will not print if the property "printMessage" is false (or empty which is the default when creating an instance of the class) but will display the message if this property is true.
Oh, yes, that will work. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 COM Component Integration에 대해 자세히 알아보기

질문:

2015년 4월 5일

댓글:

2015년 4월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by