How do I select files in a directory based upon day, month, and hour they were last modified or created?
14 views (last 30 days)
Show older comments
I have data that looks like this:
data = {"month", "day", "hour", "count"; 2, 5, 12, 25; 2, 6, 12 30; 2, 9 8, 16}
I want to read that data into matlab and then search a directory for files with a modifed date which matches the date and hour within a row, runs some analysis, and then move to the next row.Note, I have some rows which are same date and hour and others which skip multiple dates so i can't just run through the files in order. I dummied up the following code as a starting point but I am sure there's a better way to do this and it falls apart if there's more than a month of data.
data = readcsv("data.csv")
f = dir('*.wav")
for i = 1:length(data)
for n:length(f)
Day = str2num(F(2).date(:,1:2))
Hour = str2num(F(2).date(:,11:12))
if Day = data(i, 2) & Hour = data(i, 3)
audio = audio(f.name(n))
Do calcualtions
End
End
0 Comments
Answers (1)
Walter Roberson
on 9 Sep 2022
Edited: Walter Roberson
on 9 Sep 2022
f = dir("*.wav");
filenames = fullfile({f.folder}, {f.name});
fdt = datetime([f.datenum], 'convertfrom', 'datenum');
[~, ~, day] = ymd(fdt);
[hour, ~, ~] = hms(fdt);
day = day(:); hour = hour(:);
numfiles = length(f);
data = readmatrix("data.csv");
for i = 1:size(data,1)
matchidx = find(data(i,2) == day & data(i,3) == hour) .'; %need row
for n = matchidx
audiodata = audio(filenames{n});
Do calculations
end
end
See Also
Categories
Find more on Dates and Time 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!