How to add zero columns arbitrary with dates.

1 view (last 30 days)
rgreen87
rgreen87 on 10 Jun 2014
Edited: Roger Wohlwend on 10 Jun 2014
I have
a beginning date: surgery_date
And an end date: last_date.
I calculated the total number of months as follows:
date1 = datevec(surgery_date, 'dd-mmm-yy')
date2 = datevec(last_date, 'dd-mmm-yy')
% This if/else checks which date is later than the other
if(datenum(date1) > datenum(date2))
% num months is the number of months left in date2's year (ex. 2 months)+
% the number of months in date1's year (ex. 6 months) +
% the number of months in the years between (ex. 0 months).
num_months = (12 - date2(2)) + date1(2) + 12*(date1(1)-date2(1)-1)
else
%just flipped date1 and date2 from the above.
num_months = (12 - date1(2)) + date2(2) + 12*(date2(1)-date1(1)-1)
end
So num_months will give me the total number of months from surgery_date to last_date.
The data I have between these two dates is only at certain months arbitrary. So what I need to do is, start from my sugery_date and look at each next month if there is data available, if not add a column of zeros under month 1,2,3 (which ever does not have data) from the surgery date.
  1 Comment
Roger Wohlwend
Roger Wohlwend on 10 Jun 2014
Edited: Roger Wohlwend on 10 Jun 2014
All I see in your code is that you calculate the number of months between two dates. Where do you want to add a column of zeros?
By the way ... you could simplify your formula:
num_month = date1(2) - date2(2) + 12*(date1(1) - date2(1));

Sign in to comment.

Answers (0)

Categories

Find more on Dates and Time 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!