How do I separate date and time of a raw date?

>> a = untitled.startDate(1:10)
a =
10×1 categorical array
2013-07-10 00:01:00.0
2013-07-17 00:01:00.0
2013-06-29 00:01:00.0
2013-07-17 00:01:00.0
2013-05-11 00:01:00.0
2013-05-31 00:01:00.0
2013-06-06 00:01:00.0
2013-08-31 00:01:00.0
2013-05-21 00:01:00.0
2013-08-06 00:01:00.0
>> a.Format = 'dd-MMM-yyyy'
Error using categorical/subsasgn (line 87)
Attempt to assign field of non-structure array.

3 comentarios

Walter Roberson
Walter Roberson el 30 de Mzo. de 2018
What data was used to create the categorical array? Was categorical() applied to datetime() values, or was it applied to a cell array of character vectors?
aishah
aishah el 30 de Mzo. de 2018
What I did was, I imported the data into a 'table'. And I convert the data to 'Categorical Array'.
aishah
aishah el 30 de Mzo. de 2018
The imported data type is 'datetime'.

Iniciar sesión para comentar.

 Respuesta aceptada

Ahmet Cecen
Ahmet Cecen el 30 de Mzo. de 2018
Editada: Ahmet Cecen el 30 de Mzo. de 2018
arrayfun(@(x) strsplit(x,' '),string(a),'UniformOutput',false)
string(a) - this makes the data easily process-able, converts to a string array.
@(x) strsplit(x,' ') - this is a function that splits each string into parts whenever a space(' ') is located(which works for your case)
arrayfun - applies the above function to each "row" in the string array.

7 comentarios

>> arrayfun(@(x) strsplit(x,' '),string(a),'UniformOutput',false)
ans =
10×1 cell array
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
[1×2 string]
Yep, dates will be the first, time will be the second element.
ans{1}{1} %dont use ans though, assign something to it.
>> b = ans{1}{1}
b =
'2013-07-10'
aishah
aishah el 30 de Mzo. de 2018
sorry, how do I list out all 10 row with the separated elements?
Ah, here:
b = reshape([k{:}],2,length(a))'
Now:
b(:,1)
b(:,2)
aishah
aishah el 30 de Mzo. de 2018
b = reshape([k{:}],2,length(a))'
Undefined variable "k" or class "k".
celldisp(b)
b{1} =
"2013-07-10" "00:01:00.0"
b{2} =
"2013-07-17" "00:01:00.0"
b{3} =
"2013-06-29" "00:01:00.0"
b{4} =
"2013-07-17" "00:01:00.0"
b{5} =
"2013-05-11" "00:01:00.0"
b{6} =
"2013-05-31" "00:01:00.0"
b{7} =
"2013-06-06" "00:01:00.0"
b{8} =
"2013-08-31" "00:01:00.0"
b{9} =
"2013-05-21" "00:01:00.0"
b{10} =
"2013-08-06" "00:01:00.0"

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 2 de Abr. de 2018
Don't do any of that.
You've read the timestamps in as datetimes. Don't fight that. Assuming you have a table T:
T.StartTime = timeofday(T.StartDate);
T.StartDate = dateshift(T.StartDate,'start','day);

6 comentarios

Steph
Steph el 17 de Feb. de 2019
Hi, I am excatly trying to seperate datetimes into day and time columns seperately using matlab 2017b. Peter, unfortunately your code says unrecognized property:StartDate
The original poster of the question had a table or struct field named StartDate .
If you have a table which has a variable which is in datetime format then in the code that Peter showed, replace the T.StartTime with the name of your table variables
T.StartTime = timeofday(T.NameOfYourDatetimeColumnGoesHere);
T.NameOfYourDatetimeColumnGoesHere = dateshift(T.NameOfYourDatetimeColumnGoesHere, 'start', 'day');
Steph
Steph el 18 de Feb. de 2019
Dear Walter' Thank you very much for taking time to write to me.
This works perfectly.
I am trying to change this table to a double array so I can perform some regressions on it. Hence I used table2 array, now it says it can not concatenate the specified table variables, all inputs must be date/time character vectors or date/time strings.
I think after this conversion, matlab still treats these date and time columns as datetime formatted.
How can I get rid of this problem and convert the table to a double array without losing the date and time information? Do you have any idea?
I need to keep date and time information as they will be valuable for further analysis.
Walter Roberson
Walter Roberson el 18 de Feb. de 2019
Does it make sense to perform regression on a time of day? Do you not worry about differences in results if the time is represented as 100^2 * hour + 100 * minutes + seconds, compared to 60^2 * hour + 60 * minutes + seconds ? Do you not worry about differences in results if you represent date as 100^2 * month_number + 100 * day_number + two_digit_year, compared to 100^2 * two_digit_year + 100 * month_number + day_number, compared to year plus decimal fraction of a fixed-length year, compared to year plus decimal fraction of year taking into account leap year and leap seconds ?
I think you should reconsider doing regression on dates or times of day.
Perhaps it would make sense to instead calculate a duration in seconds relative to the earliest datetime ? Perhaps it would make sense to table2array(TheTable(:,3:end)) so you are not extracting the date or time information ?
Steph
Steph el 24 de Feb. de 2019
I missteated my problem I think, I am doing regressions on the data that is collected on each of the specific days. Not on the dates or days themselves
Walter Roberson
Walter Roberson el 24 de Feb. de 2019
Then you do not need to convert the dates + times or datetime objects to numeric. What you need to do is be able to group your data based upon date. You should have a look at splitapply() -- or convert the table to a timetable() object and use retime()

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 30 de Mzo. de 2018

Comentada:

el 24 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by