Link data with datenum
1 view (last 30 days)
Show older comments
Hi!
I got monitoringsdata in an axcelfile that I loaded into MATLAB. each column in excel is now a variable.
As an example;
A_time A B_time B
[1-1-2015 0:00] [20.0] [1-1-2015 0:01] [154]
[1-1-2015 0:10] [22.0] [1-1-2015 0:25] [258]
[1-1-2015 0:20] [10.0] [1-1-2015 0:33] [598]
[1-1-2015 0:30] [20.3] [1-1-2015 0:45] [458]
[1-1-2015 0:40] [14.7] [1-1-2015 0:58] [987]
[1-1-2015 0:50] [18.6]
[1-1-2015 1:00] [13.9]
I want to plot(A,B) (or the other way around).
A_time and B_time are datenum and A ad B are double's.
Most of the data is collected with an interval of 10min, but some data is random collected in time or with an interval of 1 min.
So not all arrays have the same lenght.
For example, I want 1-1-2015 0:40 to correspond with a datapoint (or the avarage of points) between 1-1-2015 0:35 and 1-1-2015 0:44.
Is it possible to do this with MATLAB? If yes, how?
I couldn't find much usefull material on the internet.
Thank you!
0 Comments
Answers (1)
dpb
on 20 Dec 2015
Alternatively to Per's solution via interpolation, if you want to aggregate data as you've mentioned and average over the time periods,
>> B=[154 258 598 458 987].'; % Sample data to accumulate
>> nDay=0.05; % a fractional day to cover the example data range
% Derive a date number series that groups at the 10-minute interval desired. Start at
% preceding day prior to midnight to cover the range
>> dfx=datenum(2014,12,31,23,54+[0:10:60*24*nDay].',0);
>> datestr(dfx) % show the values generated for clarity...
ans =
31-Dec-2014 23:54:00
01-Jan-2015 00:04:00
01-Jan-2015 00:14:00
01-Jan-2015 00:24:00
01-Jan-2015 00:34:00
01-Jan-2015 00:44:00
01-Jan-2015 00:54:00
01-Jan-2015 01:04:00
>> [~,idx]=histc(dn,dfx); % group randomly-acquired data into proper bins...
>> idx.' % again, show result
idx =
1 4 4 6 7
>> Bavg=accumarray(idx,B,[],@mean).' % accumulate averages in proper time bin
ans =
154 0 0 428 0 458 987
>> disp([datestr(dfx(1:end-1),'HH:MM ') num2str(Bavg)]) % averages vs bin times
23:54 154
00:04 0
00:14 0
00:24 428
00:34 0
00:44 458
00:54 987
>>
The actual bin centers are the "on-the-dot" 10-min intervals six minutes past the left bin boundary shown or the series 'A' times. For plotting, if one fills the zero elements with NaN they plot will not show those values; if want to not have the resulting breaks then select the elements in the array by choosing those for which
ix=Bavg(Bavg~=0);
2 Comments
dpb
on 21 Dec 2015
Edited: dpb
on 21 Dec 2015
dn is the date number array of your observations; thought that would be obvious, obviously it wasn't (obvious, that is) :) -- from the test here leading up to the above
ds={'1-1-2015 0:01';'1-1-2015 0:25';'1-1-2015 0:33';'1-1-2015 0:45';'1-1-2015 0:58'};
dn=datenum(ds,'mm-dd-yyyy HH:MM');
"how can I extend dfx?"
Change nDay in the expressions; that's why it's there.
dfx=datenum(2014,12,31,23,54+[0:10:60*24*nDay].',0); % See footnote
You can compute it from dn by comparing first/last values there. The key point to observe is the use of the vector of 10-minute increments in datenum; always use an integer step of appropriate granularity in building date number series; do not try to use floating point approximations as the rounding difference will cause failures in comparisons in use.
NB: I wrote the sample using 23:54 of the previous day from the original date/time entry to make it simpler to parse the added vector range beginning at 0. For general purpose, you can take advantage of the internal "smarts" of datenum and use the -6 minute offset from that initial point and write as
datenum(2015,1,1,0,-6+[0:10:60*24*nDay].',0)
I figured this was a little much to grasp all at once... :) You may need to add one more increment to ensure the last increment for the bin count covers the full range of the data; I'll leave that check as "exercise for the student".
BTW, in the above in place of the hardcoded yr, mo, day, hr fields you can obviously substitute computed values from datevec(dn(1)) to make it generic for any given data file.
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!