2016-03-03 82 views
0

如果時間是早上7點到下午5點之間,我能夠成功地制定一個時間表,其中輸出爲1,時間基於我的電腦。然而,週一至週日的日子也基於我的電腦。我無法找到解決方案在週一至週六輸出1,並在週日輸出0。我的代碼如下平日和週末的Matlab代碼

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 

現在,這裏的時間是正確的已經是我需要的是在工作日(週一至週日)有我需要的輸出。此外,此matlab代碼位於Simulink塊的matlab函數中。

+0

什麼版本的MATLAB – excaza

+0

@excaza R2015a。 – uhlexxxmartini

+1

請參閱['weekday'](http://www.mathworks.com/help/matlab/ref/weekday.html) – excaza

回答

1

如果你使用平日這樣,你可以爲你今天的日期指定生成0/1值:

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

,或者如果你喜歡,這一個班輪做同樣的事情,但可能不會那麼容易,如果你不熟悉的語法如下:

day_of_week_flag = (weekday(now) > 1); 

您也可以使用日期字符串這樣的轉換其他日期:

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

最後,如果您有日期/時間值的數值數組,像[2016 3 3 12 0 0],你首先需要使用datenum轉換爲串行日期,然後用平日:

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

一種替代的方法來檢查,而無需使用平日是以下內容:

time = clock; 
day_of_week = datestr(time, 8); 
if (day_of_week == 'Sun') 
    day_of_week_flag = 0; 
else 
    day_of_week_flag = 1; 
+0

非常感謝你 – uhlexxxmartini

+0

我得到這個錯誤「功能'星期一'不支持獨立的代碼生成。 coder.extrinsic的文檔以瞭解如何在模擬中使用此功能。「 – uhlexxxmartini

+0

我使用了第一個 – uhlexxxmartini