Matlab Tutorial 1
Matlab Tutorial 1
OOP in MATLAB
Lecture 5
Advanced MATLAB:
Object-Oriented Programming
Matthew J. Zahr
CME 292
Advanced MATLAB for Scientific Computing
Stanford University
1 Introduction to OOP
2 OOP in MATLAB
Class Definition and Organization
Classes
What is OOP?
OOP Fundamentals
classdef block
Contains class definition, class attributes, and defines superclasses
properties block
Defines all properties to be associated with a class instance
Defines attributes of all properties and default values
methods block
Defines methods associated with the class and their attributes
First method must have the same name as the class, called the constructor
event block
enumeration block
http:
//www.mathworks.com/help/matlab/matlab_oop/class-components.html
Class Block
Properties: Definition/Initialization
Properties: Initialization/Attributes
Methods
Examples
The remainder of this lecture will be done in the context of two examples
polynomial.m
A value class for handling polynomials of the form
p(x) = c0 + c1 x + c2 x2 + · · · + cm xm
polynomial class
classdef polynomial
%POLYNOMIAL
properties (GetAccess=public,SetAccess=private)
coeffs=0;
order =0;
end
methods
function self = polynomial(arg)
function [tf] = iszero(poly)
function [y] = evaluate(poly,x)
function [apoly] = plus(poly1,poly2)
function [mpoly] = minus(poly1,poly2)
function [ipoly] = integrate(poly,const)
function [dpoly] = differentiate(poly)
function [iseq] = eq(poly1,poly2)
function [] = plot it(poly,x,pstr,ax)
function [] = disp(poly)