Download the audio files

First, you need to download the following audio files. Save them to a folder of your choice, start Matlab and change with "cd" to that folder.

interwikiattachment:country.wav interwikiattachment:western.wav

Create the function definition

Now, go to the "File"-Menu and choose "New | M-File".

Start the with
function trial(country, fsc, western, fsw)

Comments start with a percentage character. The first comment block after the definition of the function will be displayed by Matlab whenever you type "help trial". So it is a very good idea to spend a little bit of your time with documentation before you write the first line of code.

% TRIAL plays either western or country.

% TRIAL(WESTERN,COUNTRY) displays a blank screen, ...

Copying the style of Matlab comments (upper-case function and variable names, short description in the first line, function parameters and a longer description indented below) will become very handy if you work a lot with Matlab or when you start extending the existing toolboxes.

To see that this really works, you may choose "Save" in the m-file window (or click on the disk), change to the main window, and after typing "help trial" you should see the documentation.

Play either one file at random

Append the following to the trial.m-file
if rand < 0.5,
sound(country, fsc)
else
sound(western, fsw)
end

Then, save the file and change to the Matlab command window.

Load the songs and hear either one

Type

> [c, fsc] = wavread('country.wav');

> [w, fsw] = wavread('western.wav');

Call the trial function and decided yourself which of the two is neither western nor country

> trial(c, fsc, w, fsw);

:-)


Last edited on Friday, June 6, 2003 7:06:16 am.

Valid XHTML 1.0! Valid CSS!
Page Execution took 2.793 seconds