How do I extract the audiodata from a struct that I got from dir(fullfile.....

I have a folder with 100 wav files I need to iterate through and apply different functions to
This is what I have so far
clear
% Tester_object = audioread("Recordings\Speaker Female\Eendbekdier\f_ebd_1.wav");
% plot(Tester_object);
dir("Recordings\Male Lump\");
audio_files = dir(fullfile("Recordings\Male Lump\", '*.wav'));
for i=1:height(audio_files)
name = audio_files(i).name;
Tester = audioread(name); % This is the line that is not working that I don't know how to fix
%-------- Functions applied in this square to 'Tester'
% FFT
% Frequency spectrum bands
% Comparator
%-------- These are all sorted
end
I just need to know how do I get the audiodata from within the struct that is created by dir(fullfile.....
Thank you

 Accepted Answer

"how do I get the audiodata from within the struct that is created by dir(..."
See dir documentation; dir() doesn't return files nor data, it returns a struct that contains the returned system file directory information--a significant difference. Where this misconception got started, I don't know, but it seems to be becoming more frequent...
To reference the files, dereference the struct by iterating over the number of elements within it -- it will always be a 1D struct so numel() is the appropriate counter limit.
d=dir(fullfile("Recordings\Male Lump\", '*.wav'));
for i=1:numel(d)
data=audioread(fullfile(d(i).folder,d(i).name));
...

6 Comments

My personal style would be to code more like
d=dir(fullfile("Recordings\Male Lump\", '*.wav'));
filenames = fullfile({d.folders}, {d.name});
for i = 1 : numel(filenames)
data = audioread(filenames{i});
...
end
That works, but I don't really see the point in creating the second array...other than it vectorizes the fullfile() call. But it still has to iterate through the array and dereference each element to do the work. My personal style avoids creation of temporary variables when the data are already available; yours appears to concentrate more on reducing function calls.
There's another thread currently about performance in table addressing that may make creation/extraction to arrays beneficial for efficiency, but I don't see that being an issue here.
I consider the work of selecting the files to be distinct from the work of processing the files, so I consider it more appropriate to create the complete list of files ahead of time (except sometimes when per-folder processing is being used.)
Creating all of the file names ahead of time is more compatible with the work flow of data stores. I do not tend to use datastores myself, and tend to find some parts of them over-elaborate -- but I do observe that they get used very effectively in places, especially in the deep learning tools.
"Creating all of the file names ahead of time ..."
But we've already created the full list of file names... :)
I have seen too many people incorrectly build the file name inside the for loop so I prefer to build the fully-qualified names outside the loop. Then the loop can concentrate on the data processing work instead of on how the file names are obtained.
To each his own...I would strongly suspect if those people incorrectly build the file name inside the loop they'll also do so incorrectly outside that loop...at least until they do learn correct syntax.

Sign in to comment.

More Answers (1)

This won't work when you are working with more dynamics folders but this is the solution that I found
clear
% Tester_object = audioread("Recordings\Speaker Female\Eendbekdier\f_ebd_1.wav");
% plot(Tester_object);
dir("Recordings\Male Lump\");
audio_files = dir(fullfile("Recordings\Male Lump\", '*.wav'))
anemone = audio_files(5).name
x = strcat("Recordings\Male Lump\" + anemone)
Check= audioread(x)

1 Comment

See the Answer below for the correct solution that will work regardless of where the files are located.

Sign in to comment.

Categories

Products

Release

R2021a

Asked:

on 21 Oct 2023

Commented:

dpb
on 22 Oct 2023

Community Treasure Hunt

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

Start Hunting!