read and assign audio files within a folder to variables sequentially rather than individually in matlab

You can accomplish this by using the dir() function to read all audio files within the folder, followed by a for loop to assign each audio file to a variable sequentially. Here's an example code snippet assuming all audio files are in the current working directory:

main.m
folder = './'; % specify folder directory
files = dir(fullfile(folder, '*.wav')); % read all audio files within the folder

for i = 1:length(files)
    file_path = fullfile(folder, files(i).name); % get audio file path
    audio_data{i} = audioread(file_path); % assign audio file to variable
end
297 chars
8 lines

In this code snippet, we first specify the folder directory where the audio files are stored. Then, we use the dir() function to read all audio files within the folder with the extension *.wav.

Next, we use a for loop to iterate over each audio file in files. Within the loop, we construct a full file path using fullfile() and read the audio file using audioread(). We then assign the audio file data to a variable audio_data using the loop index i.

Once the for loop completes, audio_data will contain a sequentially-ordered array of audio file data, where audio_data{1} corresponds to the first audio file in files, audio_data{2} corresponds to the second audio file, and so on.

related categories

gistlibby LogSnag