MATLAB Object-Oriented Programming

(Joyce) #1

Example Code Discussion


properties
Material
SampleNumber
Stress
Strain
end


See “Structure of the Data” on page 3-19

properties (Dependent)
Modulus
end


Calculate Modulus when queried. For
information about this code, see
“Calculate Data on Demand” on page 3-
23.

For general information, see “Set and Get
Methods for Dependent Properties” on
page 8-62

methods For general information about methods,


see “Ordinary Methods” on page 9-8

function td = TensileData(material,samplenum,...
stress,strain)
if nargin > 0
td.Material = material;
td.SampleNumber = samplenum;
td.Stress = stress;
td.Strain = strain;
end
end


For information about this code, see
“Simplifying the Interface with a
Constructor” on page 3-22.

For general information about
constructors, see “Class Constructor
Methods” on page 9-21

function obj = set.Material(obj,material)
if (strcmpi(material,'aluminum') ||...
strcmpi(material,'stainless steel') ||...
strcmpi(material,'carbon steel'))
obj.Material = material;
else
error('Invalid Material')
end
end


Restrict possible values for Material
property.

For information about this code, see
“Restrict Properties to Specific Values” on
page 3-21.

For general information about property
set methods, see “Property Set Methods”
on page 8-56.

Representing Structured Data with Classes
Free download pdf