date time from filename

22 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2022-11-16
移动:Stephen23 2023-7-29
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})
fnm = 1×3 cell array
{'P02_PRO_ST_20221115_151337'} {'P02_PRO_ST_20221115_151338'} {'P02_PRO_ST_20221115_151339'}
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
dtm = 1×3 datetime array
15-Nov-2022 15:13:37 15-Nov-2022 15:13:38 15-Nov-2022 15:13:39
tod = timeofday(dtm) % get the time of day
tod = 1×3 duration array
15:13:37 15:13:38 15:13:39
As Mathieu NOE already commented, you should replace string concatenation with FULLFILE (and also NUM2STR with SPRINTF or COMPOSE).

请先登录,再进行评论。

采纳的回答

Mathieu NOE
Mathieu NOE 2022-11-16
hello
try this code below
I use fullfile instead of your method to concat folder and basename
your path and filenames should now appear in structure S (that you can further expand to integrate other results from your computations)
fileparst is not really needed for what you want to do
% folx = 'C:\Users\A0H36019\Documents\P02' ; % my folder for my tests
folx = '/Volumes/SSD_T7/March/P02' ; % your folder for your tests
p = 2;
basename = ['P0',num2str(p),'_PRO_*.csv']; % all files have same 'prefix'
S = dir(fullfile(folx,basename)); % get list of data files in directory
%% optionnal
% S = natsortfiles(S); % sort file names into natural order (better than regular dir command) , see FEX submission :
% % %(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
filename = S(k).name % display filenames in command window : check they are sorted as needed (use natsortfiles if dir is not doing it right)
% extract the numerical arrays date and time from filename char array
ind_underscore = strfind(filename,'_');
% date
date_str = filename(ind_underscore(end-1)+1:ind_underscore(end)-1);
year = str2num(date_str(1:4)) % year
month = str2num(date_str(5:6)) % month
day = str2num(date_str(7:8)) % day
% time
ind_dot = strfind(filename,'.');
time_str = filename(ind_underscore(end)+1:ind_dot-1);
hours = str2num(time_str(1:2)) % hours
minutes = str2num(time_str(3:4)) % minutes
seconds = str2num(time_str(5:6)) % seconds
% insert your own code below
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by