Arrange data in cell array for complete data years
조회 수: 1 (최근 30일)
이전 댓글 표시
I wanted to modify the code below to show the mm/dd/yyyy in first column if col 6 of each cell has complete data set (i.e. if number of days per year = 365 or 366) but not incomplete years and col 6 values of each cell in second column in a diferent cell array "newyr".
Do you have any idea?
Thanks in advance again.
tr = readtable('test.csv','ReadVariableNames',0); % Load Data
% td = tr(1:5,:) % Diagnostic Write
isnneg = @(x) x>=0; % Function
tc = table2cell(tr);
valrow = cellfun(isnneg,tc(:,6)); % Col #6 >= 0
tcval = tc(valrow,:); % Logical Vector
% tcvq = tcval(1:5,:) % Diagnostic Write
tcdn = datenum(tcval(:,5), 'yyyy-mm-ddTHH:MM:SS'); % Create Date Numbers
tcdv = datevec(tcdn); % Create Date Vectors
% tcdq = tcdv(1:5,:); % Diagnostic Write
[uy,days,~] = unique(tcdv(:,1)); % Years In File
dend = diff([days; length(tcdn)]); % Lengths Of Years In File
yrbgn = tcdv(days,:); % First Days Of Years
yrend = tcdv([days(2:end)-1; length(tcdn)],:); % Last Days Of Years
yrvld1 = find((yrbgn(:,2) == 1) & (yrbgn(:,3) == 1)); % Valid Year Starts
yrvld2 = find((yrend(:,2) == 12) & (yrend(:,3) == 31)); % Valid Year Ends
yrvldix = yrvld1(ismember(yrvld1, yrvld2)); % Valid Years
yrvldds = days(yrvldix); % #Days In Valid Years
for k1 = 1:length(yrvldix) % Create Output Year Data
yrout{k1} = tcval(days(yrvldix(k1)):days(yrvldix(k1))+dend(yrvldix(k1))-1, :);
end
댓글 수: 3
Star Strider
2014년 11월 19일
This is a rapidly-changing question. For context, see my (as yet unaccepted) Answer at Read all the columns in a .csv file.
채택된 답변
Kelly Kearney
2014년 11월 19일
I'm not sure I see the link between your two sample files... the date/value pairs in output.csv doesn't seem to match those in test.csv. Perhaps the code below might help?
tr = readtable('test.csv','ReadVariableNames',0);
dn = datenum(tr.Var5, 'yyyy-mm-ddTHH:MM:SS');
dv = datevec(dn);
yr = unique(dv(:,1));
[tf, loc] = ismember(dv(:,1), yr);
nyr = length(yr);
% How many data points (of any value) are present per year?
nperyr = accumarray(loc, tr.Var6, [nyr 1], @length, NaN);
% How many non-negative data points are present per year?
nnonnegperyr = accumarray(loc, tr.Var6, [nyr 1], @(x) sum(x>=0), NaN);
isleap = ((mod(yr,4) == 0 & mod(yr,100) ~= 0) | mod(yr,400) == 0);
iscomplete = (~isleap & nnonnegperyr == 365) | ...
(isleap & nnonnegperyr == 366);
% Filter out dates and values for complete years only
isin = ismember(dv(:,1), yr(iscomplete));
newdata = [cellstr(datestr(dn(isin), 'dd/mm/yy')) num2cell(tr.Var6(isin))];
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!