date time from filename
21 views (last 30 days)
Show older comments
Luis Eduardo Cofré Lizama
on 16 Nov 2022
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 Comment
Stephen23
on 16 Nov 2022
Moved: Stephen23
on 29 Jul 2023
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).
Accepted Answer
Mathieu NOE
on 16 Nov 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!