2016-09-25 71 views
1

我有一個大小爲1x8734的變量timestamp。他們是最新的序列號,並說前3個元素是這樣的:將日期序列號轉換爲實際日期

1.0e+05 * 

    7.3618 7.3620 7.3620 

如果我使用datestr,我可以得到正確格式的日期和時間。

我的問題是:我需要在我的代碼中以循環形式使用這些日期和時間。第一個日期是Aug-04-2015 hh:mm:ss。假設我需要在八月份選擇所有的日期,那麼我該怎麼做呢?

+0

可以請你分享一下你得到這個時間戳矩陣的matlab代碼嗎? –

回答

0

您可以使用timestamp作爲數字,只是比較一個預定義的日期,如下所示:

timestamp = randi(180,10,1)+1.0e+05 * 7.3609; % some arbitrary dates 
starts = datenum('1-Aug-2015'); 
ends = datenum('31-Aug-2015'); 
for k = 1:numel(timestamp) 
    if timestamp(k)>=starts && timestamp(k)<=ends 
     % things to do... 
     disp(datestr(timestamp(k))) 
    end 
end 

而且你還可以去和矢量化這一點:

Aug_dates = timestamp>=starts & timestamp<=ends; 
disp(datestr(timestamp(Aug_dates))) 

因此,對於一組隨機樣日期:

11-Jul-2015 
09-Jul-2015 
18-May-2015 
29-Oct-2015 
23-Aug-2015 
12-Oct-2015 
20-Aug-2015 
14-Oct-2015 
16-Sep-2015 
05-Oct-2015 

你會在兩種情況下得到的結果:

23-Aug-2015 
20-Aug-2015 
+0

非常感謝。會嘗試這個 – kanachi

0

這是可以做到用:

timestamp = 1.0e+05 * [ 7.3618 7.3620 7.3599 7.3620 ]; 
% I have included another value, i.e. 7.3599, for better understanding. 
% 7.3599 is: 26-Jan-2015 
abc = datestr(timestamp); 

rowsabc = size(abc,1); 
def = cell(rowsabc,1); %Pre-allocation 
for k = 1:rowsabc  %Using it in a loop as you said 
    def{k}= strfind(abc(k,:),'Aug'); % finding which dates include 'Aug' 
end 

required = abc(~cellfun('isempty', def),:); %removing the rows of abc not containing 'Aug' 

或者,您可以使用避免循環:

timestamp = 1.0e+05 * [ 7.3618 7.3620 7.3599 7.3620 ]; 
abc = datestr(timestamp); 
temp = abc.'; temp=temp(:).'; 
required = abc(ceil(strfind(temp,'Aug')./11),:); 

均可以得到以下結果:

>> abc 

abc = 
04-Aug-2015 
24-Aug-2015 
26-Jan-2015 
24-Aug-2015 

>> required 

required = 
04-Aug-2015 
24-Aug-2015 
24-Aug-2015 
+0

非常感謝。會試試這個。 – kanachi