Arrange data in cell array for complete data years

1 view (last 30 days)
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 Comments
Damith
Damith on 19 Nov 2014
Edited: Damith on 19 Nov 2014
See the attached files. input file = test.csv, output = output.csv
In the input file some years do not have complete data for all 365 days, so I do not those years in output file.
Star Strider
Star Strider on 19 Nov 2014
This is a rapidly-changing question. For context, see my (as yet unaccepted) Answer at Read all the columns in a .csv file.

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 19 Nov 2014
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))];

More Answers (0)

Categories

Find more on Characters and Strings 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!