How to extract data from on a timetable dependent on a date range

조회 수: 12 (최근 30일)
I have an 81033×4 table in which the first column holds the timestamp for each date and time (format mm/dd/yy HH:mm), the second column is the datenum for each timestamp, and the other 2 columns are the recordings for each stamp
  1. How do I extract sets of data from my table depending on the timestamp? I'd like to extract the rows for each year of data (2010:2020)
  2. How do I plot timestamp vs data (Acc_Ext, Acc_Int) for each year?
The data looks like this:
(disregard the x34, I can create one that with only the first 4 columns)
An initial idea I had is:
[num,text] = xlsread('NODE2.WDH.csv', 'A1:D81034');
datenumColumn = num(:,1);
rowtotal = length(datenumColumn);
% Find datenum value for start of each year
for n = 2010:2021
Y(n) = datenum(n,1,1);
end
ind = find(Y > 0);
A = [ind',Y(ind)'];
B = A(:,2); % B is the array containing all the date strings for the start of each year
data_2010 = num((B(1)<=datenumColumn)&(datenumColumn<=B(2)), :);
data_2011 = num((B(2)<=datenumColumn)&(datenumColumn<=B(3)), :);
data_2012 = num((B(3)<=datenumColumn)&(datenumColumn<=B(4)), :);
data_2013 = num((B(4)<=datenumColumn)&(datenumColumn<=B(5)), :);
data_2014 = num((B(5)<=datenumColumn)&(datenumColumn<=B(6)), :);
data_2015 = num((B(6)<=datenumColumn)&(datenumColumn<=B(7)), :);
data_2016 = num((B(7)<=datenumColumn)&(datenumColumn<=B(8)), :);
data_2017 = num((B(8)<=datenumColumn)&(datenumColumn<=B(9)), :);
data_2018 = num((B(9)<=datenumColumn)&(datenumColumn<=B(10)), :);
data_2019 = num((B(10)<=datenumColumn)&(datenumColumn<=B(11)), :);
data_2020 = num((B(11)<=datenumColumn)&(datenumColumn<=B(12)), :);
% Create Figure for all data from Node
t = tiledlayout(3,4,'TileSpacing','Compact','Padding','Compact');
% Tiles
for i = 0:12
nexttile()
x = data_201i(:,1);
y1 = data_201i(:,2);
y2 = data_201i(:,3);
plot(x,y1,'k-');
title('2020')
hold on;
plot(x,y2,'b-');
datetick('x','mmm','keepticks') % This function read the time intervals and displays them in the x-axis
tstart = data_201i(1,1);
tend = data_201i(size(data_201i,1),1);
xlim([tstart tend])
xlabel('Month')
end
However, I can't get the data to be plotted for each iteration.

채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 8일
편집: Walter Roberson 2020년 7월 17일
However, I would suggest that you might not be asking the best question. I would suggest that you instead consider
G = findgroups(year(YourTable.Timestamp));
hold on
workfun = @(ts, Ext, Int) plot(ts, Ext, ts, Int, 'DisplayName', string(year(ts(1))));
plot_handles = splitapply( workfun, YourTable.Timestamp, YourTable.Acc_NBSP2Ext, YourTable.Acc_NBSP2Int, G);
You might want to do the work function slightly differently to be able to distinguish the line for internal and external.
  댓글 수: 2
Tomas Miguieles
Tomas Miguieles 2020년 7월 8일
Thanks for your answer. That code helps me segregate all the data by years, but how can I get a figure of multiple plots for each year? Or one plot at a time with its corresponding legend and axis labels (which in this case are the same for all plots).
Walter Roberson
Walter Roberson 2020년 7월 17일
You can make workfun into whatever it needs to be. Have it accept one parameter for each variable it will need for the plotting. List all of those same variables in order after the function handle in the splitapply() call. The function handle you pass to splitapply can be the handle to a "real" function instead of an anonymous function.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by