Arrange data in cell array for complete data years
    7 次查看(过去 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 中查找有关 Calendar 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


