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 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!