Specifying a Constructor Method
In our example, we will specify a constructor method that lets the user provide parameters to be used in the creation of the object. The constructor method often performs data initialization and validation. The object is now created with
>> s = sads(Data, Wavelength, SampleRate, Spacing, Name);
Implementing Application-Specific Methods
We will add several methods to implement application-specific operations to be performed on the data set. Most methods take the object as an input argument (for example, obj) and access the object properties by referencing this variable (for example, obj.NumSamples), as in this method:
function mag = magfft(obj, zpt)
mag = zeros(obj.NumSamples, zpt);
...
end
Although it requires additional syntax, referencing properties via the object variable can help differentiate them from local function variables, such as mag above.
Calling Methods
Methods are called just like functions, with the object passed in as one of the arguments. We can estimate the sources’ DOA angles by calling the doa method of our class.
>> angles = doa(s)
angles =
-10.1642 18.9953
The DOA angles approximate the true locations of the sources shown in Figure 1, which are -10° and 20°.
Accessing Properties with Get and Set Methods
You can validate properties or implement dependent properties by specifying associated set and get methods. Here is the get method for the NumSensors property.
function NumSensors = get.NumSensors(obj)
NumSensors = size(obj.Data, 2);
end
Get and set methods are called automatically when properties are accessed, for example with
>> N = s.NumSensors;
Specifying Methods for Existing MATLAB Functions with Overloading
Overloading lets you redefine existing MATLAB functions to work on your object by providing a function with that name in your list of methods. In our application, we will include an overloaded plot method, providing a function to visualize the data set that is familiar to many MATLAB users (Figure 5).
>> plot(s)