Automatic change values, in instance of class, by event - matlab

I would like to create a class that (for simplicity) adds two numbers as soon as I change an input parameter in an instance of the class. For simplicity I have created this class:
classdef test < handle
properties (Constant)
privatNummer = 10;
end
properties
brugerNummer;
sum;
end
methods
function obj = test()
obj.sum = method1(obj);
end
function obj = method1(obj)
obj.sum = obj.brugerNummer + obj.privatNummer;
end
end
end
How do I get it to automatically update obj.sum when I give it a new value? Currently I have to run obj.method1 every time I want to update obj.sum.
I have tried something like this (but I just can't get it working):
classdef test < handle
properties (Constant)
privatNummer = 10;
end
properties
brugerNummer;
sum;
end
methods
function obj = test()
notify(obj,'StateChange')
obj.sum = method1(obj);
addlistener(obj.brugerNummer,'Ændret nummer',#RespondToToggle.method1);
end
function src = method1(src)
src.sum = src.brugerNummer + src.privatNummer;
end
end
events
StateChange
end
end

I developed two solutions for the problems. The first relying on Dependent properties, setters and getters; the second relying on listeners and callback-functions.
First Solution:
classdef test
properties (Constant)
privatNummer = 10;
end
properties
brugerNummer;
end
properties (Dependent)
sum;
end
methods
function obj = test()
% Constructor
end
function value = get.sum(obj)
value = obj.brugerNummer + obj.privatNummer;
end
end
end
Second Solution (this was a real hassle):
classdef test < handle
properties (Constant)
privatNummer = 10;
end
properties (SetObservable)
brugerNumber;
end
properties
sum;
end
methods
function obj = test()
% constructor
addlistener(obj, 'brugerNumber', 'PostSet',#test.callbackFun);
end
end
methods (Static)
function callbackFun(~,evnt)
obj = evnt.AffectedObject;
obj.sum = obj.brugerNumber + obj.privatNummer;
end
end
end

Related

How to access property from static method

As the title says I am setting a property with the constructor and would like to access the property later as a get function that is static. How would I do this in MATLAB?
classdef Wrapper
properties(Access=public)
dataStruct
end
methods
function data = Wrapper(filePath)
if nargin == 1
data.dataStruct=load(filePath)
end
end
end
methods(Static)
function platPosition = getPlatPosition()
platPosition = dataStruct.someField
end
end
end
--------------------------
import pkg.Wrapper.*
test = Wrapper('sim.mat')
pos = test.getPlatPosition
As far as I know MATLAB doesn't have static properties like other OOP languages [ref]. Only static properties can be used inside the static methods. The closest you can get in MATLAB classes to static property is Constant property. The downside is the constant property has to be initialized and is read-only. Inside the static method, You can access read-only/constant property with class name.
classdef Wrapper
properties(Constant=true)
dataStruct=load('\path\to\sim.mat');
end
methods
function data = Wrapper()
%do something with object
end
end
methods(Static=true)
function platPosition = getPlatPosition()
platPosition = Wrapper.dataStruct.Fieldname;
end
end
end
In your case, you could accept object as an input argument of your static method.
classdef Wrapper
properties(Access=public)
dataStruct
end
methods
function data = Wrapper(filePath)
if nargin == 1
data.dataStruct=load(filePath)
end
end
end
methods(Static)
function platPosition = getPlatPosition(obj)
platPosition = obj.dataStruct.someField
end
end
end
--------------------------
import pkg.Wrapper.*
test = Wrapper('sim.mat')
pos = test.getPlatPosition(test);

How do I write a common set method for multiple properties in MATLAB

I work within a project which has several classes which define properties that use essentially the same set method. To make the code more readable, I want to implement a commonSetter method. The overall goal is to include this commonSetter method in the superclass, so that all the classes could use it.
The question was already posted here, but unfortunately, the answer is not working. I changed the code to the following, but get the error: Maximum recursion limit of 500 reached.
classdef MyClass
properties
A
B
end
methods
function mc = MyClass(a,b) % Constructor
mc.A = a;
mc.B = b;
end
function mc = set.A(mc, value) % setter for A
mc = mc.commonSetter(value, 'A');
end
function mc = set.B(mc, value) % setter for B
mc = mc.commonSetter(value, 'B');
end
end
methods(Access = protected)
function mc = commonSetter(mc, value, property)
% do some stuff
disp('Made it into the commonSetter!')
mc.(property) = value;
end
end
end
So far I know that there is an infinite loop where mc.(property) = value; calls set.A (or set.B), which in turn calls commonSetter.
In my post # MathWorks the following was suggested:
To break that loop I guess you should look at builtin() and subsasgn(). Maybe Overriding subsref and subsasgn - effect on private properties can be of some help.
Currently, I have troubles realizing the suggestions and additionally not very comfortable to overwrite subsasgn() as I'm not sure how it will affect the overall project. I would like to know, if someone has other ideas or knows how to overwrite subsasgn() safely.
To solve the recursion error, you could just let the commonSetter method output the new value instead of the object.
classdef MyClass
properties
A
B
end
methods
function mc = MyClass(a, b)% Constructor
mc.A = a;
mc.B = b;
end
function mc = set.A(mc, value)% setter for A
mc.A = mc.commonSetter(value, 'A'); % update mc.A
end
function mc = set.B(mc, value)% setter for B
mc.B = mc.commonSetter(value, 'B');
end
end
methods (Access = protected)
function new_value = commonSetter(mc, value, property) % only return the new value
% do some stuff
disp('Made it into the commonSetter!')
if value > 5
new_value = -10;
else
new_value = value;
end
end
end
end

Matlab set property from class method

I'm trying to access a class property from within a method function. When I modify the property from the constructor, the setter is called and the property is changed. But when I modify the property from another method, the property reverts to the previous value, when the function is terminated, even though the setter is called again.
What is wrong with my code, please help me!
Thanks
The code is below:
classdef random
properties
x
end
methods
function obj=random(obj)
obj.x = 2
obj.foo(1)
obj %output x:2, but it should be 1!
end
function foo(obj,A)
obj.x = A;
obj %output x:1
end
function obj = set.x(obj,newVal)
obj.x = newVal;
end
end
end
Somebody correct me if I am wrong, but I assume the obj in foo is passed by value. So it does get updated within that function space but not returned. So what works is to have it return the object and catch that in the constructor. Try:
classdef random
properties
x
end
methods
function obj=random(obj)
obj.x = 2;
obj = obj.func1(4);
disp(obj.x);
end
function [obj] = func1(obj,A)
obj.x = A;
disp(obj.x);
end
function obj = set.x(obj,newVal)
obj.x = newVal;
end
end
end

How to get A Dependent Property depending on properties of objects of different class in Matlab

See the code below:
Ex_ObjA.m
classdef Ex_ObjA
properties
a
end
methods
function Obj=Ex_ObjA(t)
Obj.a = t;
end
end
end
Ex_ObjBC.m
classdef Ex_ObjBC
properties
b
end
properties (Dependent = true, SetAccess = public)
c
end
methods
function Obj=Ex_ObjBC(t)
Obj.b = t;
end
function c=get.c(Obj,s1) % error: Get methods must have exactly one input
c = Obj.b + s1.a;
end
end
end
I tried to do following:
s1 = Ex_ObjA(2);
s2 = Ex_ObjBC(3);
s2.c
Not successful, because "Get methods must have exactly one input". So I can pass the s1.a to Ex_ObjBC to get s1.c?
c isn't really a Dependent property, it's the result of a calculation. Just get rid of the property c, and have a method c = calculatec(Obj, s1) that executes the same code as you have now.

Matlab property that automatically updates

Being new to MATLAB, I am trying to write a class where, if one of two properties changes value, a third property is automatically recalculated.
It seems events and listeners are made for this, but I just can't get the hang of their basic implementation.
My latest attempt is this
% when property a or b is altered, c will automatically be recalculated
classdef myclass < handle
properties
a = 1;
b = 2;
c
end
events
valuechange
end
methods
function obj = myclass()
addlistener(obj,'valuechange', obj.calc_c(obj))
end
function set_a(obj, input)
obj.a = input;
notify(obj, valuechange)
end
function set_b(obj, input)
obj.b = input;
notify(obj, valuechange)
end
function calc_c(obj)
obj.c = obj.a + obj.b
end
end
end
Which returns following error
Error using myclass/calc_c
Too many output arguments.
Error in myclass (line 18)
addlistener(obj,'valuechange', obj.calc_c(obj))
What am I doing wrong?
Don't you want instead to define c as Dependent, so that every time you use it you are sure that it has been updated?
Something like this
classdef myclass < handle
properties
a
b
end
properties (Dependent)
c
end
methods
function x = get.x(obj)
%Do something to get sure x is consistent
x = a + b;
end
end