Good afternoon! I'm bad at object-oriented and I need some advice. The situation is as follows: in one part of the code, I calculate the integer variable var. Then I want this variable to be declared as the default property in the class. For example,
var = a * b;
...
classdef vector
properties
v = var;
end
methods
%%
end
end
How can I implement this?

4 件のコメント

Walter Roberson
Walter Roberson 2021 年 1 月 26 日
If I recall correctly you need to declare var as a constant to be able to use it that way, and there are restrictions on how the constant can be created.
Are you trying to create a class variable, one that belongs to the class itself?
Igor Arkhandeev
Igor Arkhandeev 2021 年 1 月 26 日
I didn't fully understand your question, so I'll try to explain my thinking. I create my class objects very often. Each time I need to pass the same number as a variable. At the very beginning of my code, I calculate this variable once. In order not to pass a variable to the input every time, I want to bind it to the properties.
Mario Malic
Mario Malic 2021 年 1 月 26 日
Hey Igor,
See this video on YouTube for the brief introduction on OOP:
Object-Oriented Programming in MATLAB | Master Class with Loren Shure
From my slim knowledge of OOP, you can write the constructor method to create a class with desired value for your variable.
Igor Arkhandeev
Igor Arkhandeev 2021 年 1 月 26 日
I understand how to do this through the constructor. The thing is, I don't want to pass a variable to the input of a function every time. My code is complex and consists of a large number of nested functions. I want to avoid using global, so I need to pass my variable every time. This variable is only used in the class.

サインインしてコメントする。

 採用された回答

per isakson
per isakson 2021 年 1 月 26 日
編集済み: per isakson 2021 年 1 月 26 日

0 投票

This might help as a start
>> vec = vector;
>> vec.v1
ans =
120
>>
where
classdef vector
properties
v1 = var1_calculation;
v2 = vector.var2_calculation;
end
methods ( Static = true )
function var = var2_calculation()
a = -10;
b = 12;
var = a*b;
end
end
end
and
function var = var1_calculation()
a = 10;
b = 12;
var = a*b;
end
Then the question is where do a and b come from

4 件のコメント

Igor Arkhandeev
Igor Arkhandeev 2021 年 1 月 26 日
As you can see, such a transfer is very expensive and I would like to set the variable value in the properties once.
function f = f1(var)
%%
f = f2(var);
%%
end
function f = f2(var)
%%
f = f3(var);
%%
end
function f = f3(var)
%%
vct = vector();
vct.v = var;
%% some operations
end
Igor Arkhandeev
Igor Arkhandeev 2021 年 1 月 26 日
In this case, you need to calculate this value each time, which will take a wildly long time.
per isakson
per isakson 2021 年 1 月 26 日
I assume that a*b represents some larger costly calculation.
AFAIK: Matlab doesn't offer any better solution than the one in my answer (possibly I misunderstood your question).
The rhs of the assignments in the property block must be possible to evaluate in the base workspace. Both functions and static methods fullfill that requirement.
When vector is called for the first time the default value is calculated and the value is stored together with the class in memory. Doc says: "Evaluation of property default values occurs only when the value is first needed, and only once when MATLAB first initializes the class. MATLAB does not reevaluate the expression each time you create an instance of the class."
Igor Arkhandeev
Igor Arkhandeev 2021 年 1 月 26 日
Okay, thank you so much for your response and for your time. I should have asked that. Now I should rebuild the code in a more convenient way.

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 1 月 26 日

0 投票

If you want to initialize the value once and have that be unchangeable for the rest of the lifetime of the object, make it a Constant property.

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2021 年 1 月 26 日

回答済み:

2021 年 1 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by