How to define sequence of dates between to dates on monthly time step?

13 次查看(过去 30 天)
Hello, I want to generate sequence of dates between two dates on monthly time step, however, couldnt figure a way out. I have tried various options such as datenum, datestr, datetime etc but could not solve my problem. Here is a sample of one trial.
StartDate= datetime(1981,01,01);
EndDate=datetime(2010,12,31);
formatOut='yyyy-mm';
Date=StartDate:EndDate;
Date=datetime(Date,formatOut);
I know it won't work as the number of the argument are wrong. but I want the date in the following format. Any help would be highly appreciated.
Jan-1981
Feb-1981
Mar-1981
-------
--------
Dec- 2010

采纳的回答

Peter Perkins
Peter Perkins 2017-12-19
Hydro, I think all you need is to add a step size of one calendar month (in your code you are stepping by one day):
StartDate = datetime(1981,01,01);
EndDate = datetime(2010,12,31);
Date = StartDate:calmonths(1):EndDate;
Date.Format = 'MMM-yyyy'

更多回答(1 个)

James Knowles
James Knowles 2017-12-18
Here's a similar function I wrote for a similar purpose. It should put you on the write track and allow you to manipulate it for your own purposes.
function[m,y] = month_year(idx2,idx3)
month = ["Jan", "Feb", "March", "April", "May", "June", "July","Aug", "Sept", "Oct", "Nov", "Dec"]; year = [1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012];
m = month(idx2); y = year(idx3);
end

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by