Filter löschen
Filter löschen

Why does the rand function in the assignment statement within the class's property only work once?

1 Ansicht (letzte 30 Tage)
Define Class Properties with ConstantValues tell us:"MATLAB evaluates the expressions when loading the class"
classdef NamedConst
properties (Constant)
RN = rand(1000,1000)
end
end
if you run:
obj1=NamedConst();
x=obj1.RN;
obj2=NamedConst();
y=obj2.RN;
tf = isequal(x,y)
result:
tf =
logical
1
It is make sense,Because properties is Constant and "MATLAB evaluates the expressions when “loading the class"
Here are two questions:
1. On which line does MATLAB complete "loading the class"? “obj1=NamedConst();”? Or “x=obj1.RN;”?
2. When the properties are not Constant, shouldn't MATLAB repeatedly execute the rand function to generate different results?
Questions 1:I believe it should be completed when executing obj1 = NamedConst();, but the following code seems to prove me wrong. So which line of code does MATLAB complete "loading the class"?
obj1 size is 0 Bytes ,x size is 8000000 Bytes. If MATLAB completed "loading the class" when executing `obj1 = NamedConst();`, then the rand function should have already been executed at this point, and the size of obj1 should not be 0 Bytes.
obj1 = NamedConst();
whos;
x=obj1.RN;
whos;
Questions 2:Define a class with properties that are not Constant.
classdef ValueClassA
properties
a = rand(1000,1000);
end
end
if you run:
obj3=ValueClassA();
x=obj3.a;
obj4=ValueClassA();
y=obj4.a;
tf = isequal(x,y)
result:
tf =
logical
1
However, `tf` still equals 1. Shouldn't MATLAB re-execute the rand function when executing `obj4 = ValueClassA();` or `y = obj4.a;`? Because at this point, MATLAB should be “loading the class” again, and the property is not Constant

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 17 Sep. 2023
Bearbeitet: Bruno Luong am 17 Sep. 2023
"Shouldn't MATLAB re-execute the rand function when executing `obj4 = ValueClassA();` or `y = obj4.a"
No since the doc said
"MATLAB evaluates a default expression when the property value is first needed (for example, when the class is first instantiated). The same default value is then used for all instances of a class. MATLAB does not reevaluate the default expression unless the class definition is cleared from memory."
This also answers your Q1 "On which line does MATLAB complete "loading the class"? “obj1=NamedConst();”? Or “x=obj1.RN;”?"
obj1=NamedConst();
since it's where "the class is first instantiated"
I think you ask too many question that already answered in the doc. Just some how you don't read them correctly.
  4 Kommentare
fa wu
fa wu am 20 Sep. 2023
Thanks for your comment!
"When you have a class that has only constant properties then there is no per-instance data storage for it. It does not need a data block for each different variable of the same type in this situation. So whos reports the bytes in the instance data blocks, which is zero bytes."--------------I agree!If we follow this principle, then running the following code should produce the expected result.
obj1 = NamedConst();
obj2 = NamedConst();
whos;
Name Size Bytes Class Attributes
obj1 1000x1000 8000000 NamedConst
obj2 1x1 0 double
However, the results are clearly not like this. In actual execution, the result for obj1 is "size: 1x1 0 Bytes". Unless it's during the class's initial instantiation, the memory consumed by property evaluation expressions is counted towards the memory usage of the NamedConst class. All instantiated objects are pointers, so the memory usage for both obj1 and obj2 is 0! The memory usage of the class itself cannot be displayed using the whos command! Is my understanding correct?
Bruno Luong
Bruno Luong am 20 Sep. 2023
"The memory usage of the class itself cannot be displayed using the whos command! Is my understanding correct?"
That is correct.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 20 Sep. 2023
Verschoben: Walter Roberson am 20 Sep. 2023

Named variables in MATLAB have a symbol table entry that appears to be a fixed size, and which appear to hold little more than the name and a pointer to descriptor of the variable.

Unnamed temporary variables are created all the time in MATLAB: for example A+1+B creates an unnamed temporary variable to hold the results of A+1. The pointers to those get passed around to do the real work.

A descriptor of a variable contains information about the dimensions of the variable and the class of a variable and whether it is complex or global. If the variable is not empty then there is one (possibly two historically for complex) pointers to data blocks.

When you ask for whos of a variable, the size of the variable descriptor is not included. For example

A = 1.23; whos A

will show that A is 1x1 double and 8 bytes. Each location of a double takes 8 bytes, A has exactly 1 element, 1*8=8 and whos A will report the 8. So whos reports the (occupied) size of the data blocks associated with a variable.

When you have a class that has only constant properties then there is no per-instance data storage for it. It does not need a data block for each different variable of the same type in this situation. So whos reports the bytes in the instance data blocks, which is zero bytes.

Meanwhile the descriptor for the variable contains the class name, and the class has been loaded into memory. The information such as list of method names and pointer to implementing code do not need to be stored per instance, only once for the class, and that class definition location is also where the class statics are stored. whos on an instance is not going to show the size of that shared class data anymore than it would show how much memory is allocated for the method implementations.

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by