How can I access two subset of a string in a table field (to convert into two datetime fields) ?

2 visualizaciones (últimos 30 días)
Hello,
I can not manage to find a vectorized way to deal with a long table string to datetime conversion.
I explain:
I have a 4496835x3 table. My second field (Dates) has been imported into a field containing cells.
This field is composed of uniform strings containing two dates (beggining-ending). Example: '2016.11.07 21:00-2016.11.07 22:00' (a string of 33 characters)
I want to create two other fields in datetime format (Let's say Date1 and Date2).
I can already manage it by looping on each row:
InputFmt='yyyy.MM.dd HH:mm';
for j=1:height(data)
data.Date1(j)=datetime(data.Dates{j}(1:16),'InputFormat',InputFmt);
data.Date2(j)=datetime(data.Dates{j}(18:end),'InputFormat',InputFmt);
end
which is very long to compute.
I also tried a kind of pre-allocation, using:
data.Date1(:,1)=DefaultDate;
data.Date2(:,1)=DefaultDate;
before the loop, where DefaultDate is an odd date in datetime format. But it is still heavy to run...
That's why I am wondering of a way to vectorize this datetime conversion, but I can not find a way to access each 'sub-string' independently. Indeed, I try different combinations of braces/curly braces, indexing, but nothing goes as I wish. I have to admit that struct and cell indexing are still things that I don't catch well either.
Example of codes that I tried to access the "Date1-Part":
test.Dates{:}(1:16)
test.Dates{:,1}(1:16)
{data.Dates(1:16)}
[data.Dates(1:16)]
and probably some others.
I hope it is clear enough, and thanks if anyone can help.

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Nov. de 2016
splitzees = regexp(data.Dates, '-', 'split');
as_cell = vertcat(splitzees{:});
datetime(as_cell, 'InputFormat', InputFmt)
That would give you an N x 2 array of datetime objects

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by