image thumbnail

What's New for Object-Oriented Programming in MATLAB Webinar - Code Examples

by

 

10 Apr 2008 (Updated )

Code examples used in "What's New for Object-Oriented Programming in MATLABĀ®" Webinar

Test Inherited Class

Test Inherited Class

You can create a customized class by specifying that your class inherits from another class.

Contents

Look at inherited class

I have a class called sadsT in another package called inheritedImp that inherits from the class we just made. I will import it.

clear
clear import
import inheritedImp.sadsT
loadparameters % and get the parameters again

Classdef

(open inherited.sadsT) To inherit from another class use the angle bracket in the classdef file e.g. classdef < superclass1. Here you see we specify it inherits from the sads class using its full name with package.

Add properties

We add the test run property.

Constructor

For the constructor, we take in all the same arguments as before plus the new one test_run and pass most of them to the super class constructor first using a special syntax to create the object, then we set the other property. We create an object with:

sT=sadsT(Data, Wavelength,SampleRate,Spacing,Name,1) % providing other arguments
sT = 

inheritedImp.sadsT
package: inheritedImp

properties:
    TestRunNumber: 1
             Data: [64x16 double]
       SampleRate: 3.3333e+007
          Spacing: 1.2500
             Name: 'Sensor Array Amplitudes'
                c: 300000000
       NumSensors: 16
       NumSamples: 64


Here we see the other property defined in the object

sT
sT = 

inheritedImp.sadsT
package: inheritedImp

properties:
    TestRunNumber: 1
             Data: [64x16 double]
       SampleRate: 3.3333e+007
          Spacing: 1.2500
             Name: 'Sensor Array Amplitudes'
                c: 300000000
       NumSensors: 16
       NumSamples: 64


Plot method

For the plot method, I'm choosing to call the superclass's plot method. When you call the plot method you see the property is added.

plot(sT)

Multiple inheritance

You can inherit from multiple classes too using the syntax < superclass & superclass2

Clean up

clear import

Contact us