## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# Processing a Set of Files

There continue to be questions on the MATLAB newsgroup regarding processing a set of files. So, for the record, and even though Steve covered this topic in his blog, I thought I'd get an answer on record here as well.

### Setup

I should start a clean workspace and with no WAV-files in my blog publishing directory.

clear all
delete *.wav

### Problem Statement

Suppose I want to convert sounds stored in MATLAB MAT-files to files saved in WAV format for Windows. Without explictly hardcoding in the filenames, here's a way to proceed.

### Collect the MAT-files Containing Sounds

matfiles = dir(fullfile(matlabroot,'toolbox','matlab','audiovideo','*.mat'))
matfiles =
7x1 struct array with fields:
name
date
bytes
isdir


### Check out the Files

We can see from the length of matfiles that I have 7 MAT-files. I know I don't care about the first one in this case so I am starting my analysis with the second one.

load(matfiles(2).name)
whos
  Name           Size                    Bytes  Class

Fs             1x1                         8  double array
matfiles       7x1                      2435  struct array
y          13129x1                    105032  double array

Grand total is 13390 elements using 107475 bytes



Loading the data places the MAT-file contents into a structure from which I can extract the information I need and write it back out. The sound files that MATLAB ships with store the data in y and the sampling frequency in Fs. Let's look at the first signal.

N = length(y);
plot((1:N)/(N*Fs),y), title(matfiles(2).name(1:end-4))

### Loops over the Files

for ind = 2:length(matfiles)
wavwrite(data.y,data.Fs,matfiles(ind).name(1:end-4));
end
Warning: Data clipped during write to file:gong
Warning: Data clipped during write to file:splat
Warning: Data clipped during write to file:train


dir *.wav
chirp.wav     handel.wav    splat.wav
gong.wav      laughter.wav  train.wav



### Read a File Back for Verification

I'll double-check the last file I just read in and wrote out. ind is still set despite no longer being in the for loop. Let's check both the frequencies and the signals themselves.

[ywav, Fswav] = wavread(matfiles(ind).name(1:end-4));
eqFreqs = isequal(Fswav, data.Fs)
datadiff = norm(ywav-data.y)
eqFreqs =
1