MATLAB Answers

For Loop through .wav files

22 views (last 30 days)
Karianne Kapfer
Karianne Kapfer on 2 Mar 2021
Commented: Jan on 3 Mar 2021
NOTE I AM BRAND NEW TO MATLAB SO IF POSSIBLE PLEASE EXPLAIN AND/OR USE SIMPLE CODE
My professor asked me to write a code that will loop through my folder of four files creating a waveform, powerspectrum, and waveform for each file as well as saving those figures as a PDF before moving onto the next file.
This code is supposed to utitlize the listing = dir("PathName') function
Here is what I have
How on earth do I get this to work?
My Current Code:
dir("C:\Users\Karianne Kapfer\MATLAB\Assignment2\Soundfiles") %%the directory my files are in
for listing = dir("C:\Users\Karianne \MATLAB\Assignment2\Soundfiles")
%%Need to loop through the files
%%the graphs I am trying to make
fs = 64000
subplot(3,1,1)
plot(listing) <- This plot is also not working for some reason
xlabel("Samples")
ylabel('Amplitude')
title('Waveform')
hold on
subplot(3,1,2)
pspectrum(listing, fs)
xlabel("Frequency (kHz)")
ylabel('Amplitude')
title('Power Spectrom')
hold on
subplot(3,1,3)
s = spectrogram(listing)
spectrogram(listing,1024,[],[],fs,'yaxis')
publish('Assignment2MultipleFiles.mlx', 'pdf')
hold off
end
  2 Comments
Karianne Kapfer
Karianne Kapfer on 2 Mar 2021
Done, I find this harder to read but if its helpful to others that is all that matters!

Sign in to comment.

Answers (1)

Jan
Jan on 2 Mar 2021
Edited: Jan on 2 Mar 2021
Folder = 'insert your folder here'; % Sure that there is a sapce after your name?
FileList = dir(fullfile(Folder, '*.wav'));
for iFile = 1:numel(FileList)
File = fullfile(Folder, FileList(iFile).name);
% then audioread will help
end
  2 Comments
Jan
Jan on 3 Mar 2021
No, FileList is a struct, which contains data about the file. You cannot plot the names and sizes of the files.
You want to import the contents of the files to get the data. As I have written already, audioread() can do this.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!