MATLAB Object-Oriented Programming

(Joyce) #1
Example Code Discussion
function m = get.Modulus(obj)
ind = find(obj.Strain > 0);
m = mean(obj.Stress(ind)./obj.Strain(ind));
end

Calculate Modulus property when
queried.

For information about this code, see
“Modulus Property Get Method” on page
3-23.

For general information about property
get methods, see “Property Get Methods”
on page 8-60.
function obj = set.Modulus(obj,~)
fprintf('%s%d\n','Modulus is: ',obj.Modulus)
error('You cannot set Modulus property');
end

Add set method for Dependent Modulus
property. For information about this code,
see “Modulus Property Set Method” on
page 3-24.

For general information about property
set methods, see “Property Set Methods”
on page 8-56.
function disp(td)
fprintf(1,'Material: %s\nSample Number: %g\nModulus: %1.5g\n',...
td.Material,td.SampleNumber,td.Modulus)
end

Overload disp method to display certain
properties.

For information about this code, see
“Displaying TensileData Objects” on page
3-24

For general information about
overloading disp, see “Overloading the
disp Function” on page 18-42
function plot(td,varargin)
plot(td.Strain,td.Stress,varargin{:})
title(['Stress/Strain plot for Sample',...
num2str(td.SampleNumber)])
ylabel('Stress (psi)')
xlabel('Strain %')
end

Overload plot function to accept
TensileData objects and graph stress
vs. strain.

“Method to Plot Stress vs. Strain” on page
3-25
end
end
end statements for methods and for
classdef.

3 MATLAB Classes Overview

Free download pdf