Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Peter Perkins
Peter Perkins il 10 Ago 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.

Accedi per commentare.

Risposta accettata

Jinsong Han
Jinsong Han il 8 Ago 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.

Più risposte (0)

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by