Automatically update class properties

10 visualizaciones (últimos 30 días)
Simon Baeuerle
Simon Baeuerle el 14 de Nov. de 2022
Comentada: Matt J el 16 de Nov. de 2022
Hey there,
I've got the following problem: I've got a class with two properties A and B, which dependent on other properties hmatrix and n_FFT. Here is a minimal example:
classdef example
properties
hmatrix
n_FFT
A
B
end
methods
function obj = example()
obj.B = 10.*2^6;
obj.hmatrix = [0,1];
obj.n_FFT = 2^6;
end
function obj = set.hmatrix(obj,value)
obj.hmatrix = value;
obj.A = value.*obj.B;
end
function obj = set.n_FFT(obj,value)
obj.n_FFT = value;
obj.B = 10.*value;
obj.A = obj.hmatrix.*obj.B;
end
end
end
The property B depends on n_FFT and the property A depends on hmatrix and n_FFT (default values in constructor help when initializing).
Due to performance issues, I only want to recompute B and especially the property A (its more complicated than shown here), if the variables hmatrix and n_FFT change. Otherwise, I could do this by simply writing some get methods for the variables.
This code above runs, but the code analyzer is complaining that set methods should not access other properties, due to load order issues.
Is there a clean, "proper" way to do this, where I do not use get methods? I looked into dependent and transient properties but could not figure out how to use them without writing get methods, which are called every time.
Thank you very much! I

Respuesta aceptada

Matt J
Matt J el 14 de Nov. de 2022
Editada: Matt J el 14 de Nov. de 2022
  2 comentarios
Simon Baeuerle
Simon Baeuerle el 16 de Nov. de 2022
Thank you for the link and for your reply! However, my problem is not an infinite recursion. The code analyzer tells me to not access other properties in a set method, which are not dependent. I can't see how this example helps me w.r.t. that concern. Do you have any suggestions. Sorry - I am a little bit lost here.
Matt J
Matt J el 16 de Nov. de 2022
classdef example
properties
hmatrix_
n_FFT_
A_
B_
end
properties (Dependent)
hmatrix
n_FFT
A
B
end
methods
function obj = example()
obj.B = 10.*2^6;
obj.hmatrix = [0,1];
obj.n_FFT = 2^6;
end
function obj = set.hmatrix(obj,value)
obj.hmatrix_ = value;
obj.A_ = value.*obj.B_;
end
function obj = set.n_FFT(obj,value)
obj.n_FFT_ = value;
obj.B_ = 10.*value;
obj.A_ = obj.hmatrix.*obj.B_;
end
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by