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

12 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2021-12-13
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 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by