date time from filename
이전 댓글 표시
Hi there I need to read the date and time from a series of filenames (4) that are not fully sync. First I'm struggling to only get the date and time (e.g., P02_PRO_ST_20221115_151337.csv). In the example, the part with '20221115_151337'. Secondly, I'm also struggling to convert the second half (i.e.,'151337') to time, which is the variable part across my 4 files. I need the latter as it is the start of the timestamp (in milliseconds) of my timeseries within the files and which I will use to sync the signals in the files. I tried the following:
p = 2;
folx = {'/Volumes/SSD_T7/March/'};
basename = strcat(folx,'P0',num2str(p),'/','P0',num2str(p),'_PRO_'); % all files have same 'prefix'
listoffiles = dir(strcat(basename,'*','.csv')); % JUST NOT WORKING
Since the above is not working the I cannot move to use the following to extract the date and time for each of my files;
[~,name,~] = fileparts(filename);
Many thanks for your help
Eduardo
댓글 수: 1
First lets create some fake data files just for testing the code:
writematrix(1:3,'P02_PRO_ST_20221115_151337.csv')
writematrix(4:6,'P02_PRO_ST_20221115_151338.csv')
writematrix(7:9,'P02_PRO_ST_20221115_151339.csv')
Now lets get a list of the filenames:
P = 2;
D = '.'; % absolute/relative path to where the file are saved
B = sprintf('P0%d_PRO_ST_*.csv',P);
S = dir(fullfile(D,B));
[~,fnm,~] = fileparts({S.name})
It is easy to convert the datestamp to DATETIME objects and then the time of day as DURATION objects:
tmp = regexp(fnm,'\d+_\d+$','match','once');
dtm = datetime(tmp,'InputFormat','uuuuMMdd_HHmmss') % convert to DATETIME
tod = timeofday(dtm) % get the time of day
As Mathieu NOE already commented, you should replace string concatenation with FULLFILE (and also NUM2STR with SPRINTF or COMPOSE).
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!