フィルターのクリア

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

12 ビュー (過去 30 日間)
Syed
Syed 2021 年 12 月 13 日
回答済み: Steven Lord 2021 年 12 月 13 日
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 Center および File ExchangeConstruct 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