convert cell array into date and time string

6 次查看(过去 30 天)
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.

采纳的回答

Karim
Karim 2023-1-4
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'}
date_hne = 1×6 cell array
{'2014'} {'04'} {'01'} {'23'} {'46'} {'44.998300'}
% concat the data and convert into a string
date_hne_string = string([date_hne{:}])
date_hne_string = "20140401234644.998300"
% convert into a datetime
date_hne_dt = datetime(date_hne_string, 'InputFormat', 'yyyyMMddHHmmss.SSSSSS')
date_hne_dt = datetime
01-Apr-2014 23:46:44
  1 个评论
Jorge Luis Paredes Estacio
Thank you very much. How would be the case for this string:
'2021-04-23T17:00:19.860000Z' or '2021-04-23T17:00:19Z' to convert it into 2021/04/23 17:00:19. A general command should be applied because sometimes the amount of decimals in the seconds may vary.

请先登录,再进行评论。

更多回答(1 个)

Eric Sofen
Eric Sofen 2023-1-4
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")
d = datetime
01-Apr-2014 23:46:44

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by