How do I select files in a directory based upon day, month, and hour they were last modified or created?

14 ビュー (過去 30 日間)
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

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 9 日
編集済み: Walter Roberson 2022 年 9 月 9 日
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
  2 件のコメント
Benjamin Colbert
Benjamin Colbert 2022 年 9 月 9 日
This doesn't run for me. Get two errors:
"Unable to resolve the name 'd.folder'."
I assume that's a typo so changed d.folder to f.folder.
Then i get:
Error in noise_scan_beforeafter (line 2)
filenames = fullfile({d.folder}, {d.name});

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by