MATLAB Object-Oriented Programming

(Joyce) #1
MATLAB software includes audio data that you can load and use to create an
audioplayer object. This sample load audio data, creates the audio player, and plays the
audio:

load gong Fs y
gongSound = audioplayer(y,Fs);
play(gongSound)

Suppose that you copy the gongSound object handle to another variable (gongSound2):

gongSound2 = gongSound;

The variables gongSound and gongSound2 are copies of the same handle and, therefore,
refer to the same audio source. Access the audioplayer information using either
variable.

For example, set the sample rate for the gong audio source by assigning a new value to
the SampleRate property. First get the current sample rate and then set a new sample
rate:

sr = gongSound.SampleRate;
disp(sr)

8192

gongSound.SampleRate = sr*2;

You can use gongSound2 to access the same audio source:

disp(gongSound2.SampleRate)

16384

Play the gong sound with the new sample rate:

play(gongSound2)

Handle Objects Modified in Functions


When you pass an argument to a function, the function copies the variable from the
workspace in which you call the function into the parameter variable in the function’s
workspace.

1 Using Object-Oriented Design in MATLAB

Free download pdf