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

조회 수: 5 (최근 30일)
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
%file.
directory={'Y:\wav files\'}; %folder where wav files are
tlo=4.0;
thi=119.0;
fileend='_2min'
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
rand_num=randperm(10)+f;
end
end

채택된 답변

Image Analyst
Image Analyst 2020년 7월 14일
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);
end
Adapt as needed.

추가 답변 (1개)

Nipun Katyal
Nipun Katyal 2020년 7월 14일
Hi,
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));
end

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by