0

I were able to successfully made a schedule in which the output is 1 if time is between 7 AM-5PM and otherwise 0, time is based on my computer. However the day Monday-Sunday is based on my computer as well.. I cant find the solution to put an output 1 on Monday-Saturday and output 0 on Sunday. The code I have is below

function y = IsBetween5AMand7PM
coder.extrinsic('clock');
time = zeros(1,6);
time = clock;
current = 3600*time(4) + 60*time(5) + time(6); %seconds passed from the beginning of day until now
morning = 3600*7; %seconds passed from the beginning of day until 7AM
evening = 3600*17; %seconds passed from the beginning of day until 5PM
y = current > morning && current < evening;

end

Now the time here is correct already what I need is for the day (Monday-Sunday) to have my needed output. Also this matlab code is inside a matlab function on Simulink block.

  • What version of MATLAB – sco1 Mar 03 '16 at 13:30
  • 1
    See [`weekday`](http://www.mathworks.com/help/matlab/ref/weekday.html) – sco1 Mar 03 '16 at 13:50
  • @excaza tried that already but it locates both the weekend (saturday and sunday) whereas what I need is to have sunday alone to have a 0 output.. could you put the code of how will you do it.. please – uhlexxxmartini Mar 03 '16 at 13:54

1 Answers1

1

If you use weekday like this, you can generate a 0/1 value as you specified for today's date:

if (weekday(now) > 1)
   day_of_week_flag = 1;
else
   day_of_week_flag = 0;

or if you like, this one-liner does the same thing, but may not be as easy to read if you're not familiar with the syntax:

day_of_week_flag = ( weekday(now) > 1);

You can also use date-strings like this to convert other dates:

day_of_week_flag = ( weekday('01-Mar-2016') > 1 )

Finally, if you have a numeric array of date/time values, like [2016 3 3 12 0 0], you first need to convert to a serial date using datenum, then use weekday:

time = clock;
day_of_week_flag = ( weekday(datenum(time)) > 1);

An alternate way to check without using weekday is the following:

time = clock;
day_of_week = datestr(time, 8);
if (day_of_week == 'Sun')
   day_of_week_flag = 0;
else
   day_of_week_flag = 1;
gariepy
  • 3,576
  • 6
  • 21
  • 34