MATLAB: How are the dependent attributes in a class calculated right after I create them

MATLAB

I follow the example in the documentation regarding the dependent attributes:
The class is defined as:
classdef TensileData
properties
Material
SampleNumber
Stress
Strain
end
properties (Dependent)
Modulus
end
methods
function td = TensileData(material,samplenum,stress,strain)
if nargin > 0
td.Material = material;
td.SampleNumber = samplenum;
td.Stress = stress;
td.Strain = strain;
end
end
function m = get.Modulus(obj)
ind = find(obj.Strain > 0);
m = mean(obj.Stress(ind)./obj.Strain(ind));
end
end
end
When I create an object of this class:
td = TensileData('carbon steel',1,[2e4 4e4 6e4 8e4],[.12 .20 .31 .40])
MATLAB shows me that the 'Modulus' is being calculated:
TensileData with properties:
Material: 'carbon steel'
SampleNumber: 1
Stress: [20000 40000 60000 80000]
Strain: [0.1200 0.2000 0.3100 0.4000]
Modulus: 1.9005e+05
However, the documentation says that the dependent attributes get method determines the property value when the property is queried. The dependent attributes are not supposed to be calculated upon creation of the object.

Best Answer

This reason of this behavior is because showing all the attributes in an object is being considered as querying all the attributes. And therefore, the 'get.Modulus(obj)' is being executed.
To show this behavior, the customer can add an extra line in the 'get.Modulus(obj)' method:
> disp('get.Modulus is being called');
Matlab will print 'get.Modulus is being called' in MATLAB Command Window every time the 'get.Modulus(obj)' method is being executed.
Then, execute the following two lines:
> %%This line will print 'get.Modulus is being called'
> td = TensileData('carbon steel',1,[2e4 4e4 6e4 8e4],[.12 .20 .31 .40]);
>
> %%This line will NOT print 'get.Modulus is being called'
> td
You can see the first line will NOT print 'get.Modulus is being called', but the second line will.
The following sample shows where you can add the line to display 'get.Modulus is being called':
classdef TensileData
properties
Material
SampleNumber
Stress
Strain
end
properties (Dependent)
Modulus
end
methods
function td = TensileData(material,samplenum,stress,strain)
if nargin > 0
td.Material = material;
td.SampleNumber = samplenum;
td.Stress = stress;
td.Strain = strain;
end
end
function m = get.Modulus(obj)
ind = find(obj.Strain > 0);
m = mean(obj.Stress(ind)./obj.Strain(ind));
disp('get.Modulus is being called')
end
end
end