Combine date vector into single column

I have a n X 6 matrix, with time data:
1st col = yyyy (e.g. 2016)
2nd col = mm (e.g. 10 for Oct)
3rd col = day (e.g. 1)
4th col = hour
5th col = min
6th col = seconds
I need to combine them into a n X 1 (e.g. 1-Oct-2016 00:00:00). How can I do that?
My initial data comes in the form of serial date (e.g. 4.264400001156250e+04 for 1-Oct-2016 00:00:00)
I used this function to convert into a date vector, but am stuck from here:
date_vec = datevec(datestr(x2mdate(raw_data)));
Should I be doing this? My goal is to export to excel for it to be read properly (need to plot a time series). Currently when it goes to the next day, instead of showing up the date of the next day, it simple wraps back to AM of the same day.
e.g.
42644.99999
42644.99999
42645
42645

2 Comments

Stephen23
Stephen23 on 19 Jan 2017
Edited: Stephen23 on 19 Jan 2017
Why not just use datenum to convert the dates to serial date numbers ? Then each date is a scalar, and you can put them all into one column, just like you ask for.

Sign in to comment.

Answers (2)

Would something like this work?
raw_data = [2016 10 1 3 45 50;
2017 1 1 4 15 20]
arrayfun(@datestr,datenum(raw_data),'UniformOutput',false)

5 Comments

>> datestr( raw_data, 'yyyy-mm-dd HH:MM:SS' )
ans =
2016-10-01 03:45:50
2017-01-01 04:15:20
>> version
ans =
9.0.0.341360 (R2016a)
this is an issue, as the end result is of type char (n X 19). I need to combine it into column 1 of a matrix A (type double). When I use your suggestion:
date_1 = datestr(raw_data, 'yyyy-mm-dd HH:MM:SS' );
A(:,1) = date_1;
it says: Subscripted assignment dimension mismatch.
The end result of my answer is a cell array of individual dates, not a character array. You could also have just done
datenum(raw_data)
to have the numeric version of the dates. I am somehow losing track of exactly what you want the output to look like.
Actually, my raw dates come in the Excel serial format (e.g. 4.264400001156250e+04 = 01-Oct-2016 00:00:00)
I need to convert into such a format -> 01-Oct-2016 00:00:00 and store it into the first column of a matrix A ( type double).
I tried your initial method:
dates(:,:) = arrayfun(@datestr,datenum(raw_data(:,:)),'UniformOutput',false)
But because it was cell array, when I tried storing into the matrix:
A(:,1) = dates(:,1);
It says:
The following error occurred converting from cell to double: Error using double Conversion to double from cell is not possible.
How can I transfer the data to that matrix?
Stephen23
Stephen23 on 15 Jan 2017
Edited: Stephen23 on 15 Jan 2017
"I need to convert into such a format -> 01-Oct-2016 00:00:00 and store it into the first column of a matrix A ( type double)."
A string with multiple characters has multiple columns (one per character), so cannot be put into one column of another matrix, unless that other matrix is a cell array. You could store this string in a character array (with multiple columns) or a cell array (with one column), but certainly not in a numeric array with one column. Not unless you convert it to a serial date number first.

Sign in to comment.

Unless you're using a version of MATLAB older than R2014b, you should be using datetimes:
>> datetime(4.264400001156250e+04,'ConvertFrom','Excel')
ans =
datetime
01-Oct-2016 00:00:00

3 Comments

I have datetime object in the format 15-12-2014 00:05:00
I want to extract the date 15-12-2014 as a seperate variable and 00:05:00 as another variable so that I can merge these two variables into a double (matrix)
how can I do that?
The literal answer is timeofday, subtraction, and some form of convertTo, but you probably DON'T actually want to do that. Unless yu have a specific reason, stick with datetimes.
Also, it's considered bad form to tack a completely unrelated question on the end of a two-year-old thread.
Yes Peter you are right, but I thought it was related to the original question, as I also have each element of date time seperately (6 arrays) and was trying to get 2 different columns one for each date and time.
I will open a new thread if I can not find the answer I am looking for.
Tnx.

Sign in to comment.

Asked:

on 13 Jan 2017

Commented:

on 18 Feb 2019

Community Treasure Hunt

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

Start Hunting!