Randomly select one file, every 10 files, and randomise where you take that file from

5 views (last 30 days)
I have a database of thousands of .wav files which I am looking to subset. For every 10 files, I would like to select one file, but since these files are recording at specific time intervals, I want to avoid the scenario that every 10th file occurs at the same time interval. I want to randomise where I select every 10th file from, between 1:10. So far I have come up with the following but it means that at the end of the loop, it crashes as (I think) the range of random numbers doesn't match the range of available files to draw from. I would like to apply this loop to several files of folders so I can leave it running for days without interferring.
%19 June 2020, Louise Wilson
%Plot spectrograms for a folder of .wav files, save each .png in the same
%folder as the .wav files.
%For speed-just do every 10th file, but randomise which file is every 10th
directory={'Y:\wav files\'}; %folder where wav files are
for a=1:length(directory) %for 1:number of folders
folder=char(directory(a)); %select a'th folder
b=dir(fullfile(folder,'*.wav')); %list all .wavs in folder
files=length(b); %number of .wavs in folder
%every 10th file, but randomise where it's taken from between 1-10
rand_num=randperm(10); %generate random number between 1:10
f=0; %first lot of random numbers is between 1-10
for c=1:10:files %for every bunch of 10 files
d=rand_num(1); %select first of those random numbers to choose random file
disp(b(d).name); %display filename
%commands to .wav file go here
f=f+10; %iterate range of random numbers to correspond with file loop

Accepted Answer

Image Analyst
Image Analyst on 14 Jul 2020
Use fileDatastore() and randperm():
topLevelFolder = 'D:\One Drive\WaveFiles'; % Wherever you want.
fds = fileDatastore(topLevelFolder, 'ReadFcn', @audioread, 'IncludeSubfolders', true, 'FileExtensions', '.wav')
% Get the total number of files.
numFilesTotal = length(fds.Files)
% Get 1/10th of that as the number we want to use.
nunFilesToUse = round(numFilesTotal / 10)
% Get the indexes randomly from within the whole set.
indexes = sort(randperm(numFilesTotal, nunFilesToUse), 'ascend');
% Loop over all the chosen files, processing them.
for k = 1 : length(indexes)
thisFileName = fds.Files{indexes(k)};
fprintf('Now processing "%s".\n', thisFileName);
Adapt as needed.

More Answers (1)

Nipun Katyal
Nipun Katyal on 14 Jul 2020
I see that you need to select a file randomly at intervals of 10, hope this logic might help you,
b = 1000;
for i = 1:10:b
fprintf('Choose %dth file\n',i+randi([0,9],1));


Find more on Programming in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!