% Use an audioDatastore to point to all audio files in a folder
% Set IncludeSubFolders to true to read subfolders too
ads = audioDatastore(pwd,'IncludeSubfolders',true);
% Read the first 5 seconds of a file of your choice
% This is example code - change it to your use case
index = 39;
[sig,Fs] = audioread(ads.Files{39});
sig = sig(1:Fs*5);
% Read all the files
% If you have too many files, and the signals don't all fit in memory, use
% a for loop instead (use read(ads) to read the datastore one file at a
% time)
allData = readall(ads);
% Find the max correlation of each signal with sig
m = cellfun(@(x)max(xcorr(x, sig)), allData, 'UniformOutput', false);
% Pick the index of max of max value
[~,index2] = max(cell2mat(m));
fprintf('Actual: %d\n',index) %39
fprintf('Expected: %d\n',index2) %39