MATLAB Answers

BOB
0

Merge two columns of a cell array into one

Asked by BOB
on 23 Feb 2018
Latest activity Commented on by Jan
on 24 Feb 2018
Hi,
I have two columns of cell values:
I want to merge these two columns to produce one third column in the format yyyy-mm-dd hh-mm-ss.
Thanks for any help!

  0 Comments

Sign in to comment.

1 Answer

Answer by KSSV
on 23 Feb 2018

c1 = {'2104-04-14'; '2014-04-14'} ;
c2 = {'13:17:08'; '13:16:45'} ;
datetime(strcat(c1,{' '},c2))

  7 Comments

Jan
on 24 Feb 2018
@John BG: It is not useful to invent problems, that the OP does not have.
why is that when reading the resulting merged columns, the format
YYYY-MM-DD is reversed?
This is just the default format of the datetime object for the output, not for the internal storage, as explained in the documentation. If you want to keep the output in the original format:
c1 = {'2104-04-14'; '2014-04-14'} ;
c2 = {'13:17:08'; '13:16:45'} ;
B = datetime(strcat(c1, {' '}, c2))
B.Format = 'yyyy-MM-dd hh:mm:ss'
B =
2×1 datetime array
2104-04-14 01:17:08
2014-04-14 01:16:45
well, if you add the line
B.Format = 'yyyy-MM-dd hh:mm:ss'
then it complies with the question requirement:
YYYY-MM-DD
If you do not add that line, then you are supplying years for days and days for years, it would be kind of odd, wouldn't it?
the order matters, if one goes to cash machine and asks for
£301.- (YYYY-MM-DD)
but only gets
£103.- (DD-MM-YYYY)
wouldn't one complain?
Jan, no inventions, just reading the question.
Jan
on 24 Feb 2018
@John BG: Bob said "Thank you!" already, which sounds like the problem was solved. Then datetime works already. Maybe you did not understand, how datetime stores its data internally, but it does not matter what you set as display format. A calculation like "£301.- (YYYY-MM-DD)" is nonsense. If you need the value of a datetime object, you would e.g. use:
301 - day(B(1))
and for the output of day(B) it does not matter, if the date is displayed as YYYY-MM-DD or DD-MM-YYYY. This is the idea of datetime compared to the old serial date numbers or date vectors, where the display and the meaning of the dates and times were related.

Sign in to comment.