MATLAB Examples

Test Simple Object


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.


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 = 


    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.

ans =


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.


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.

catch ME
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.

ans =


whos s
  Name      Size            Bytes  Class          Attributes

  s         1x1                64  sads_simple              

ans =


Finding Available Data Items (Properties)

The properties command can be used to find object properties.

Properties for class sads_simple:


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