I have this code creating a list of months, however the output is on the last month, Dec. Is it possible to create a matrix of the individual month in order to incorporate into another code?

3 ビュー (過去 30 日間)
charlotte wilson
charlotte wilson 2016 年 4 月 21 日
編集済み: tpn 2022 年 10 月 4 日
ii = 1; %counter
% creating a list of months
while ii<=12
t = datetime(2016,ii,01):calweeks(5):datetime(2016,ii+1,01);
m(ii) = (month (t,'shortname'));
ii = ii+1;
end

回答 (2 件)

Nikhil Vyas
Nikhil Vyas 2016 年 4 月 25 日
I'm not sure about what output you require from this code so I'm assume that you just want a cell array with month values.
It's considered good to avoid using loops in MATLAB. Thus, the code you've written is generally considered inefficient. I have made the modifications to get the output that I assume you want.
temp = datetime(2016,01,01):calmonths(1):datetime(2016,12,31)
month_cell_array = month(x, 'name');
month_cell_array now contains the names of the months.
In case you expected some other output, let me know. :)

tpn
tpn 2022 年 10 月 4 日
編集済み: tpn 2022 年 10 月 4 日
string(month(datetime(1,1:12,1),'shortname'))
ans = 1×12 string array
"Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
This should work well

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by