loops - Creating new array of dates/data with missing dates/data added in Matlab -
i have 4 columns of river flow data in format (year,month,day,data). want check through data ensure there not missing chunks , if there are, create new data array ('newdates') missing dates (column 1) added in, along nan values missing data (column 2).
i checking missing data subtracting datenum of previous day, datenum of current day (if greater 1, there data missing). code below works, omitting last day of data each time. loop have generated check each row length of data minus 1, because if allow length of data, won't able check difference between 2 last dates, date vector not large enough.i know isn't overly complex thing do, can't seem figure out. appreciated. code below:
%load sample file of data in format year,month,date,riverflow
>data=load('dmf_21002_279631_q_complete_matlab.csv'); >yr=data(:,1); >mth=data(:,2); >day=data(:,3); >flow=data(:,4); >dates=datenum([yr,mth,day]); >icounter=1;
%this counter indexing 'newdates'
>ndcounter=0 >for =1:(length(dates)-1); >ndcounter=ndcounter+1; >if dates(i+1)-dates(i)==1; >newdates(ndcounter,1)=dates(i,1); >newdates(ndcounter,2)=data(i,4); >elseif (dates(i+1)-dates(i))~=1; >newdates(ndcounter,1)=dates(i,1); > newdates(ndcounter,2)=data(i,4);
%count number of days of data missing array
>daysmissing(icounter,1)=(dates(i+1)-dates(i))-1;
%create missing datenums adding ii on previous
%datenum (do length of data missing)
>for ii=1:daysmissing; >newdates((i+ii),1)=((newdates(i))+ii); > newdates((i+ii),2)=nan; > end >ndcounter=ndcounter+daysmissing; >disp('missing data found'); > missingidx(icounter)=i; >icounter=icounter+1; >end >end >newdates(newdates==-999)=nan;
i advise timeseries
class. on timeseries
objects, can call resample()
create missing values.
mwe:
dates = datenum([2016 2016 2016], [4 4 4], [10 11 13]); data = [0 1 3]; ts = timeseries(data, dates); ts2 = ts.resample(datenum([2016 2016 2016 2016], [4 4 4 4], [10:13])); figure(1); hold on; ts.plot('bo--'); ts2.plot('rx:');
Comments
Post a Comment