How can I write all month's last dates for a given period of time?

조회 수: 2 (최근 30일)
Giorgi
Giorgi 2015년 1월 28일
댓글: Giorgi 2015년 1월 28일
Hello guys. Well I have daily time series form 1990-2015
% code
b=726867;
a=9125;
for i=1:a
b=b+1;
BigMatrix(i,1)=b;
end
BigMatrix=cellstr(datestr(BigMatrix));
Now I want to make another variable where I have only last date of months for a same period of time.
Thanks in advance

채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 1월 28일
Hi,
first of all: you don't need the loop
BigMatrix = b + (1:a);
BigMatrixStr = cellstr(datestr(BigMatrix));
For the last day of a month you have different possibilities. One is to use datevec and note, that the last day of a month is one day before the first of the next month:
% add one day and convert to date vectors:
BigMatrixVec = datevec(BigMatrix+1);
% remove all that are not the first of a month
BigMatrixVec(BigMatrixVec(:,3)~=1, :) = [];
% and subtract that one day again:
BigMatrixStrLast = cellstr(datestr(datenum(BigMatrixVec)-1));
Or use the datetime object that comes with R2014b:
dt = datetime(BigMatrix, 'ConvertFrom', 'datenum');
t = unique(dateshift(dt, 'end', 'month'));
% remove the last month if it's past the last dt
t(t>dt(end)) = [];
Titus

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2015년 1월 28일

You could use lbusdate to get the last business day of the month specifying no weekend days:

weekend = zeros(1,7); % that's depressing :)
web(fullfile(docroot, 'finance/lbusdate.html'))

Andrei Bobrov
Andrei Bobrov 2015년 1월 28일
BigMatrix = cellstr(datestr(b+(1:a)'));

카테고리

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