How do I integrate datetime data ?

조회 수: 5 (최근 30일)
Slawomir Kania
Slawomir Kania 2018년 5월 8일
댓글: Peter Perkins 2018년 7월 5일
I have a plot of datetime data with x axis being datetime (yyyy-MM-dd-HH:mm:ss) and y axis being my data.
my data file looks like this:
2018-03-12-17:50:43 836.00
2018-03-12-17:50:48 871.00
2018-03-12-17:50:54 1116.00
2018-03-12-17:50:59 906.00
2018-03-12-17:51:04 834.00
2018-03-12-17:51:10 927.00
2018-03-12-17:51:15 950.00
2018-03-12-17:51:21 827.00
2018-03-12-17:51:27 999.00
2018-03-12-17:51:33 1088.00
and I convert this data for plotting using this formula
fid = fopen('.\2018-04-02.txt','r');
c = textscan(fid,'%s%f');
fid = fclose(fid);
data = c{2};
%t=datetime(c{1},'Format','HH:mm:ss');
t=datetime(c{1},'Format','yyyy-MM-dd-HH:mm:ss');
figure;
plot(t,data);
Which gives me nice plot of my data depending on time.
I need to convert said data into an incrementing function portraing integration of continous changes adding up depending on time. I tried using the trapz function to conduct numerical integration to no success. The datetime format I'm using does not correlate with trapz function. Also changing the time format into double does not work either.
Basically the plot needs to portray incrementation of my data depending on time.
Any ideas ?
Many thanks in Advance

채택된 답변

Walter Roberson
Walter Roberson 2018년 5월 8일
You can use datenum() on datetime objects to get serial date numbers. Or you could subtract the first datetime entry from them all in order to get duration objects, which you could then use seconds() on to get a numeric version.

추가 답변 (1개)

Slawomir Kania
Slawomir Kania 2018년 6월 20일
So I found my solution!
What I did was making a table of small integrations using the datenum format and trapz function:
cz=datenum(t);
for i=2:1:size(t)
q1(i)=trapz([cz(i-1),cz(i)],[data1(i-1),data1(i)]);
end
then I transfered data into incrementing table:
for i=2:1:size(t)
q2(i)=q2(i-1)+q1(i);
end
end result gave me incrementing integration:
  댓글 수: 1
Peter Perkins
Peter Perkins 2018년 7월 5일
Walter's second suggestion, i.e.
>> t = datetime(2018,7,5,0,0,100*sort(rand(1,5)))
t =
1×5 datetime array
05-Jul-2018 00:00:03 05-Jul-2018 00:00:31 05-Jul-2018 00:00:43 05-Jul-2018 00:01:09 05-Jul-2018 00:01:35
>> dt = seconds(t - t(1))
dt =
0 28.265 40.43 66.038 91.578
is better from a numerical standpoint, because it avoids round-off due to datenum using "day" as the unit. May not matter much, hard to say.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by