extracting files having names with the same date from a dataset

조회 수: 1 (최근 30일)
Salma fathi
Salma fathi 2022년 1월 31일
편집: Stephen23 2022년 1월 31일
I am having some txt files as a training dataset for a modele I'm trying to build.
if we asumed that the txt files names has the format YYYYMMDDHHmm as 196611110428.
if I would like to extract files that have the month=12, day=05, hour=22. discarding the years and the minutes, How I can possibly do that?

채택된 답변

Stephen23
Stephen23 2022년 1월 31일
편집: Stephen23 2022년 1월 31일
Here is one approach, tested on the attached files:
P = '.'; % absolute or relative path to where the files are saved
S = dir(fullfile(P,'*.txt'));
[~,F,~] = fileparts({S.name});
T = datetime(F,'InputFormat','uuuuMMddHHmm');
X = T.Month==12 & T.Day==5 & T.Hour==22
X = 1×6 logical array
0 1 1 0 0 1
{S(X).name} % training set
ans = 1×3 cell array
{'196612052228.txt'} {'196612052258.txt'} {'202212052228.txt'}
{S(~X).name} % not training set
ans = 1×3 cell array
{'196611110428.txt'} {'196612110428.txt'} {'202211110428.txt'}

추가 답변 (2개)

Walter Roberson
Walter Roberson 2022년 1월 31일
dinfo = dir('*120522.txt');
filenames = {dinfo.name};
  댓글 수: 1
Stephen23
Stephen23 2022년 1월 31일
편집: Stephen23 2022년 1월 31일
That does not take into account the minutes.
Adding another asterisk will not fix that ... if only DIR supported ? to match one character :(

댓글을 달려면 로그인하십시오.


Sambit Supriya Dash
Sambit Supriya Dash 2022년 1월 31일
a = 196611110428;
strA = string(a);
d = datetime(strA,'InputFormat','yyyyMMddHHmm');
disp(d)
Month = month(d);
Day = day(d);
Hour = hour(d);

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by