フィルターのクリア

Create a monthly date vector of serial numbers

38 ビュー (過去 30 日間)
Sandra
Sandra 2012 年 1 月 13 日
回答済み: Steven Lord 2018 年 8 月 31 日
Hi everyone,
I spent hours trying this and I am sure it must be easy but I can't get my head around it. What I want is to create a vector that contains serial numbers for each month for 30 years, so that I can use this vector for the x axis of a plot. (so e.g. I want Jan 1990, Feb 1990, March 1990 etc in serial numbers)
why doesn't something simple like this work? datenum({'01-Jan-1900':'01-Nov-2011'})
Any help is appreciated Thanks so much Sandra

採用された回答

Oleg Komarov
Oleg Komarov 2012 年 1 月 13 日
You are "almost" right, but what you're saying to matlab with,
datenum({'01-Jan-1900':'01-Nov-2011'})
is:
"Take the string '01-Jan-1900' (which has numeric value of 48 49 45 74 97 110 45 49 57 48 48) and apply the colon operator till the string '01-Nov-2011' (48 49 45 78 111 118 45 50 48 49 49)"
what you have to do is first convert the string to serial dates and then apply the colon...but this approach is not precise because you want one month intervals and months are irregular.
The solution is the following:
datenum(1900,1:120,1)
The months in excess of the first year will augment the year to 1901 and so on.
  2 件のコメント
Fangjun Jiang
Fangjun Jiang 2012 年 1 月 13 日
Excellent! Sounds like a useful trick!
Sandra
Sandra 2012 年 1 月 13 日
Thanks so much, I knew there must be a trick:-)

サインインしてコメントする。

その他の回答 (4 件)

Walter Roberson
Walter Roberson 2012 年 1 月 13 日
[Y,M] = meshgrid(1900:1929, 1:12);
TheSerialDates = datenum([Y(:), M(:), ones(numel(Y),1)]);
  3 件のコメント
Walter Roberson
Walter Roberson 2012 年 1 月 13 日
Thanks for catching that. I have fixed it now. I think that's the first time I ever found a use for meshgrid() !
Suene
Suene 2015 年 7 月 23 日
This solution also worked for me. Many thanks!

サインインしてコメントする。


Fangjun Jiang
Fangjun Jiang 2012 年 1 月 13 日
x=bsxfun(@(Month,Year) datenum(Year,Month,1),(1:12).',1980:2010);
x=x(:);
diff(x)
  1 件のコメント
Suene
Suene 2015 年 7 月 23 日
thanks for sharing! It worked for me! :)

サインインしてコメントする。


Steven Lord
Steven Lord 2018 年 8 月 31 日
With datetime and calendarDuration there are a couple different ways to create this vector.
The first technique just adds calendar months to the starting date.
startDate = datetime('January 1, 1990');
V1 = startDate + calmonths(0:30*12-1); % 30 years = 30*12 calendar months
The second technique adds between 0 and 11 calendar months and between 0 and 29 calendar years to the starting date. This uses implicit expansion to add the months and years together to form a matrix. I reshape that matrix when I compute V2 to make it the same size and orientation as V1. If you wanted something more like a planner view, look at V2planner.
startDate = datetime('January 1, 1990');
oneYearInMonths = calmonths(0:11);
thirtyYearsInYears = calyears(0:29);
monthsPlusYears = oneYearInMonths.' + thirtyYearsInYears;
V2 = startDate + reshape(monthsPlusYears, 1, []);
V2planner = startDate + monthsPlusYears;
This next one takes advantage of the fact that the datetime function is not limited to accepting only month numbers between 1 and 12. The 13th month of 1990 is actually January 1991.
monthlist = 1:30*12;
V3 = datetime(1990, monthlist, 1);
Do these give the same result?
isequal(V1, V2)
isequal(V2, V3)
isequal(V1, V3)

Navid Ghajarnia
Navid Ghajarnia 2018 年 8 月 31 日
This is another answer to this question:
t1 = datetime(1900,01,01);
t2 = datetime(2011,11,01);
t(:,1) = t1:t2;
Dates_Daily = [year(t), month(t)];
Dates_Monthly = unique(Dates_Daily,'rows');

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by