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

조회 수: 13 (최근 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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by