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 Simple Object

Test Simple Object

Contents

Inspect Sad_simple classdef file

We are going to create a class to describe the sensor array data set. First we will look at specifying the properties. You define a class in MATLAB with a class definition file.

  • Classdef file: To make a new class definition file, we can start with a blank M file or use the template in current directory browser (right-click new class). We will name the class sads_simple. The filename must match the classdef name.
  • File structure: The class def which begins with the classdef keyword followed by a class name that must equal the file name. It contains blocks of code, denoted by keywords and end statements that describe different aspects of the class.
  • Properties block: The first block is the properties block, where we see all the data items listed. We can also specify initial values for them if we wanted to.
  • Location: The classdef file must be on you path, in an @ directory on your path or in a package on your path. We will discuss why this is useful later. It can be just in your current directory, but this not recommended. As the CD can change which and can cause problems if you have objects based on these classes in your workspace. There definitions go out of scope and you will get a warning.

Creating the Data Set Object

Here we create a sads object (an instance of the sads class) with.

s=sads_simple;

You can display the object by typing its name just like other variables. You can see the properties and their values which are currently empty.

s
s = 

sads_simple

properties:
    NumSensors: []
    NumSamples: []
          Data: []


Access Data Items (Properties)

All properties are public by default. You can use tab completion at the command window or in the editor to get prompted for available properties.

s.NumSensors=16;
s.NumSensors
ans =

    16

Inspecting Objects in the Variable Editor

You can view and edit the object in the variable editor by double clicking on it in the workspace browser.

openvar('s')

Adding Data Items (Properties)

With objects, you can't arbitrarily add a new property by specifying it and assigning a value. The following code will error (as data has been miss-spelt) which we will catch, display and continue.

try
    s.data=rand(64,16);
catch ME
    disp(ME.message)
end
No public field data exists for class sads_simple.

Identifying the Data Set

The data set can be identified as a unique object using the class and isa functions and the whos command.

class(s)
ans =

sads_simple

whos s
  Name      Size            Bytes  Class          Attributes

  s         1x1                64  sads_simple              

isa(s,'sads_simple')
ans =

     1

Finding Available Data Items (Properties)

The properties command can be used to find object properties.

properties(s)
Properties for class sads_simple:

    NumSensors
    NumSamples
    Data

Getting Help on Data Items (Properties)

You can use the help command, specifying the class name and property name.

help sads_simple.NumSensors
NumSensors - Number of Sensors

Contact us