Filter löschen
Filter löschen

how can I auto-link two properites in a matlab class

12 Ansichten (letzte 30 Tage)
Syed
Syed am 13 Dez. 2021
Beantwortet: Steven Lord am 13 Dez. 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

Akzeptierte Antwort

Steven Lord
Steven Lord am 13 Dez. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by