Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comment
Peter Perkins
Peter Perkins on 10 Aug 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.

Sign in to comment.

Accepted Answer

Jinsong Han
Jinsong Han on 8 Aug 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.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!