If-Else Statement of Time in Matlab -
hi made code in have input of computer time , date goes
function [y, m, d, h, mn, s] = fcn() coder.extrinsic('now'); coder.extrinsic('datevec'); y = 0; m = 0; d = 0; h = 0; mn = 0; s = 0; [y, m, d, h, mn, s] = datevec(now); end
it works fine. tried make block controller have output of 1 between 7am-5pm , output 0 if not within time , code goes this
function y = fcn(u) u = datestr(7:00am:5:00pm) if u = datestr(7:00am:5:00pm) y=1; else y=0; end
but error occured. please me figure out what's wrong. thank you
first of all, first function you've made built in matlab clock
.
regarding problem, there plenty of possible approaches problem, think easiest 1 count seconds passed beginning of day.
using clock
command vector of current time in format: [year month day hour minute second]
. so, time passed beginning of day 3600*time(4) + 60*time(5) + time(6)
, i.e. 3600 times hours plus 60 times minutes , plus seconds. 00:00:00 7:00:00 7*3600 seconds passed. 00:00:00 17:00:00 17*3600 seconds passed. therefore, can compare these values find whether it's between 7am , 5pm or not:
function y = isbetween5amand7pm time = clock; current = 3600*time(4) + 60*time(5) + time(6); %seconds passed beginning of day until morning = 3600*7; %seconds passed beginning of day until 7am evening = 3600*17; %seconds passed beginning of day until 5pm y = current > morning && current < evening;
hope helps.
Comments
Post a Comment