Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the first business date of a week in MATLAB?

We can use fbusdate to get the first business day of a month:

Date = fbusdate(Year, Month);

However, how do we get the first business day of a week?

As an example, during the week that I'm posting this, Monday 09/07/2017 was a holiday in the US:

isbusday(736942) % = 0

How do I determine that the first business day for this week would be the next day 736943?

like image 211
JohnAndrews Avatar asked Sep 07 '17 15:09

JohnAndrews


People also ask

How do I get the day of the week from a date in Matlab?

DayNumber = weekday( D ) returns a number representing the day of the week for each element in D . [ DayNumber , DayName ] = weekday( D ) additionally returns abbreviated English names for the day of the week, in DayName .

Is Matlab a business day?

Busday = isbusday( Date ) returns logical true ( 1 ) if Date is a business day and logical false ( 0 ) otherwise. Busday = isbusday(___, Holiday , Weekend ) , using optional input arguments, returns logical true ( 1 ) if Date is a business day, and logical false ( 0 ) otherwise.

How do I get the current date in Matlab?

Date = today returns the current date as a serial date number. Date = today( outputType ) returns the current date using an optional outputType . The type of output is determined by an optional outputType variable input.


2 Answers

I'm not aware of a builtin function that returns the first working day of a week, but you can obtain it by requesting the next working day after Sunday:

busdate(736941); % 736941 = Sunday 09/03/2017
like image 88
m7913d Avatar answered Oct 25 '22 07:10

m7913d


Your desired fbusdateweek function can be done in one line using just the function weekday to get the first Sunday of the week then busdate to get the next business day after that:

dn = 736942;  % Date number for any day in a week
Date = busdate(dn-weekday(dn)+1);


Note: busdate uses the function holidays by default to get all holidays and special nontrading days for the New York Stock Exchange. If necessary, you can define an alternate set of holidays for busdate to use as follows:

holidayArray = ...;  % Some set of date numbers, vectors, or datetimes
Date = busdate(dn-weekday(dn)+1, 1, holidayArray);

This way you can define a set of localized holidays.

like image 39
gnovice Avatar answered Oct 25 '22 05:10

gnovice