load multiple audio files into an array in matlab

To load multiple audio files into an array in MATLAB, you can use the audioread function in a loop to read each file and append it to a cell array. Here is an example code that reads all the files in a folder and stores them in a cell array:

main.m
folder_path = 'path/to/folder';
files = dir(fullfile(folder_path, '*.wav')); % or '*.mp3' or any other supported format
num_files = numel(files);

audio_data = cell(num_files, 1);
for i = 1:num_files
    file_path = fullfile(folder_path, files(i).name);
    [audio_data{i}, fs] = audioread(file_path);
end

% now you can access each audio file using audio_data{i}
364 chars
12 lines

In this example, folder_path is the path to the folder containing the audio files, files is a list of all the files in the folder that match the specified format, and num_files is the total number of files.

Inside the loop, audioread is used to read the audio data and sampling rate from each file, which is then stored in a cell array audio_data.

Note that audioread returns the audio data as a matrix, where each column represents a channel in the audio file (e.g. left and right). You can extract a specific channel by indexing the matrix like this: audio_data{i}(:, 1) for the first channel.

Also, if the audio files have different lengths or sampling rates, you may need to pad or resample them to ensure they can be stored in the same array.

related categories

gistlibby LogSnag