MATLAB Object-Oriented Programming

(Joyce) #1
Define the property get method in a methods block using only default attributes.

methods
function modulus = get.Modulus(obj)
ind = find(obj.Strain > 0);
modulus = mean(obj.Stress(ind)./obj.Strain(ind));
end
end

This method calculates the average ratio of stress to strain data after eliminating zeros in
the denominator data.

MATLAB calls the get.Modulus method when the property is queried. For example,

td = TensileData('carbon steel',1,...
[2e4 4e4 6e4 8e4],...
[.12 .20 .31 .40]);
td.Modulus

ans =
1.9005e+005

Modulus Property Set Method

To set the value of a Dependent property, the class must implement a property set
method. There is no need to allow explicit setting of the Modulus property. However, a
set method enables you to provide a customized error message. The Modulus set method
references the current property value and then returns an error:

methods
function obj = set.Modulus(obj,~)
fprintf('%s%d\n','Modulus is: ',obj.Modulus)
error('You cannot set the Modulus property');
end
end

Displaying TensileData Objects


The TensileData class overloads the disp method. This method controls object display
in the command window.

The disp method displays the value of the Material, SampleNumber, and Modulus
properties. It does not display the Stress and Strain property data. These properties
contain raw data that is not easily viewed in the command window.

3 MATLAB Classes Overview

Free download pdf