MATLAB Object-Oriented Programming

(Joyce) #1
function modifySampleRate(audioObj,sr)
audioObj.SampleRate = sr;
end

Create an audioplayer object and pass it to the modifySampleRate function:

load gong Fs y
gongSound = audioplayer(y,Fs);
disp(gongSound.SampleRate)

8192

modifySampleRate(gongSound,16384)
disp(gongSound.SampleRate)

16384

The modifySampleRate function does not need to return a modified gongSound object
because audioplayer objects are handle objects.

Determine If an Object Is a Handle


Handle objects are members of the handle class. Therefore, you can always identify an
object as a handle using the isa function. isa returns logical true ( 1 ) when testing for a
handle variable:

load gong Fs y
gongSound = audioplayer(y,Fs);
isa(gongSound,'handle')

To determine if a variable is a valid handle object, use isa and isvalid:

if isa(gongSound,'handle') && isvalid(gongSound)


end

Deleted Handle Objects


When a handle object has been deleted, the handle variables that referenced the object
can still exist. These variables become invalid because the object they referred to no
longer exists. Calling delete on the object removes the object, but does not clear handle
variables.

For example, create an audioplayer object:

1 Using Object-Oriented Design in MATLAB

Free download pdf