Borrar filtros
Borrar filtros

How can i sum +hours(6) in a date cell?

1 visualización (últimos 30 días)
Lucas Barboza
Lucas Barboza el 8 de Ag. de 2017
Comentada: Peter Perkins el 10 de Ag. de 2017
Good morning!
I am making a project and I create, because it is more easy in the global context of the project, a cell with a date array and a double array. And i want to sum 6 hours in a date array present in my cell, but i don't know how.
For instance:
<DateStrings = {'2014-05-26 08:00';'2014-08-03 20:00'}; t = datetime(DateStrings,'InputFormat','yyyy-MM-dd HH:mm')
num=[2;3];
Ex=cell(2, 2);
Ex(:,1)=num2cell(t(:)); Ex(:,2)=num2cell(num(:)); >
And i want to sum 6 hour in the dates presents in the cell Ex.
  1 comentario
Peter Perkins
Peter Perkins el 10 de Ag. de 2017
You say, "because it is more easy in the global context of the project, a cell with a date array and a double array", and that may be true, but you should seriously consider using a table, or even a timetable (if you have R2016b or later). Tables are designed for mixed data, and are much more convenient to use, and use less memory, than cell arrays.

Iniciar sesión para comentar.

Respuesta aceptada

Jinsong Han
Jinsong Han el 8 de Ag. de 2017
On the datetime arithmetic documentation page for MATLAB R2017a, you can add 6 hours to a datetime object by using the hours method while specifying the amount of hours to add. Thus, to add 6 hours to all dates (assuming they are all in the first column of your cell array), you could do something like the following:
for x = 1:size(Ex, 1)
Ex{x, 1} = Ex{x, 1} + hours(6)
end
This loops through each value in the first column and adds 6 hours to the stored datetime object.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by