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

1 次查看(过去 30 天)
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 个评论
Peter Perkins
Peter Perkins 2017-8-10
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.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by