convert cell array into date and time string
11 views (last 30 days)
Show older comments
Jorge Luis Paredes Estacio
on 4 Jan 2023
Commented: Jorge Luis Paredes Estacio
on 4 Jan 2023
Hello, I have the following cell (1x6) that will be extracted from different text files of accelerometers records. For example:
date_hne =
1×6 cell array
{'2014'} {'04'} {'01'} {'23'} {'46'} {'44.998300'}
I would like to convert it into a string to recognize date and time like 2014-04-01 23:46:45
I would appreciate your help.
0 Comments
Accepted Answer
Karim
on 4 Jan 2023
See below for one method on how to do this
% reconstruct the cell array from the OP
date_hne = {'2014' '04' '01' '23' '46' '44.998300'}
% concat the data and convert into a string
date_hne_string = string([date_hne{:}])
% convert into a datetime
date_hne_dt = datetime(date_hne_string, 'InputFormat', 'yyyyMMddHHmmss.SSSSSS')
More Answers (1)
Eric Sofen
on 4 Jan 2023
As much as I love string, there's no need to do an extra type conversion. Just concatenate the elements of the cell into a char vector and pass that directly to datetime.
C = {'2014','04','01','23','46','44.998300'};
d = datetime([C{:}],"InputFormat","uuuuMMddHHmmss.SSSSSS")
0 Comments
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!