how can I auto-link two properites in a matlab class
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Syed
el 13 de Dic. de 2021
Respondida: Steven Lord
el 13 de Dic. de 2021
Hi all,
I am relatively new to the object-oriented programming in Matlab (though I have vast experience working with non-object-oriented Matlab).
Here's my question.
I have two parameters in a class that I want to link. For example, y = f(x) and as soon as x changes, 7 should change as well.
As soon as 'x' changes, 'y' should reflect that change as well. The value of 'y' is depended on the opearion of function 'my_func' below. How can I do that?
I am flexibile in the implementation and if it can be done without methods (ie within properties), I am happy with that too.
classdef MyClass
properties
x
y
end
methods
function b = my_func(a)
if x <10
y = 1;
elseif x>=10 && x < 50;
y=2;
else
y = 3;
end
end
end
end
0 comentarios
Respuesta aceptada
Steven Lord
el 13 de Dic. de 2021
Make x and/or y dependent properties, either having the get.y method perform its computations using the value of the x property or having both depend on a third property. For example, if you had area and perimeter properties of an object they could both be Dependent and use the value of a property that stores the coordinates of the vertices of that object.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Classes en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!