Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Lucas Barboza
Lucas Barboza le 8 Août 2017
Commenté : Peter Perkins le 10 Août 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 commentaire
Peter Perkins
Peter Perkins le 10 Août 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.

Connectez-vous pour commenter.

Réponse acceptée

Jinsong Han
Jinsong Han le 8 Août 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.
  1 commentaire
Lucas Barboza
Lucas Barboza le 8 Août 2017
Thanks! Help me a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by