Test Simple Object
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.
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 = sads_simple properties: NumSensors:  NumSamples:  Data: 
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 = 16
You can view and edit the object in the variable editor by double clicking on it in the workspace browser.
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.
The data set can be identified as a unique object using the class and isa functions and the whos command.
ans = sads_simple
Name Size Bytes Class Attributes s 1x1 64 sads_simple
ans = 1
The properties command can be used to find object properties.
Properties for class sads_simple: NumSensors NumSamples Data
You can use the help command, specifying the class name and property name.
NumSensors - Number of Sensors