MATLAB Object-Oriented Programming

(Joyce) #1

Representing Structured Data with Classes


In this section...
“Objects as Data Structures” on page 3-19
“Structure of the Data” on page 3-19
“The TensileData Class” on page 3-20
“Create an Instance and Assign Data” on page 3-20
“Restrict Properties to Specific Values” on page 3-21
“Simplifying the Interface with a Constructor” on page 3-22
“Calculate Data on Demand” on page 3-23
“Displaying TensileData Objects” on page 3-24
“Method to Plot Stress vs. Strain” on page 3-25
“TensileData Class Synopsis” on page 3-26

Objects as Data Structures


This example defines a class for storing data with a specific structure. Using a consistent
structure for data storage makes it easier to create functions that operate on the data. A
MATLAB struct with field names describing the particular data element is a useful way
to organize data. However, a class can define both the data storage (properties) and
operations that you can perform on that data (methods). This example illustrates these
advantages.

Background for the Example

For this example, the data represents tensile stress/strain measurements. These data are
used to calculate the elastic modulus of various materials. In simple terms, stress is the
force applied to a material and strain is the resulting deformation. Their ratio defines a
characteristic of the material. While this approach is an over simplification of the process,
it suffices for this example.

Structure of the Data


This table describes the structure of the data.

Representing Structured Data with Classes
Free download pdf