divide cell array into date and time columns
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a cell array with 6 columns and 10000 rows. The first column contains date and time in the format eg. '31/12/2001 12:57:03'. I want to seperate the first column into two seperate columns, one for date and the other for time. Then I want to convert the date to dateserial number.
thanks
댓글 수: 0
채택된 답변
Walter Roberson
2017년 10월 7일
temp = regexp(YourCellArray(:,1), '\s+', 'split');
datecol = cellfun(@(C) C{1}, temp, 'uniform', 0);
timecol = cellfun(@(C) C{2}, temp, 'uniform', 0)
serialdates = datenum(datecol);
If you do not actually need the time column, then this can be abbreviated down to
serialdates = datenum( regexprep(YourCellArray(:,1), '\s.*', '') );
댓글 수: 2
Walter Roberson
2017년 10월 8일
YourCellArray{1,1} = '31/12/2001 12:57:03';
YourCellArray{2,1} = '30/12/2001 13:18:03';
then
temp = regexp(YourCellArray(:,1), '[\s:]+', 'split');
datecol = cellfun(@(C) C{1}, temp, 'uniform', 0);
hourcol = cellfun(@(C) C{2}, temp, 'uniform', 0);
mincol = cellfun(@(C) C{2}, temp, 'uniform', 0);
since_midnight = str2double(hourcol) * 60 + str2double(mincol);
... or,
temp = datevec(YourCellArray(:,1));
since_midnight = temp(:,4) * 60 + temp(:,5);
추가 답변 (1개)
Peter Perkins
2017년 10월 13일
Think about using datetime and duration instead of datenum:
>> dt = datetime({'30/12/2015 15:54:30';'30/12/2015 15:54:30';'30/12/2015 15:54:30'},'InputFormat','dd/MM/yyyy HH:mm:ss')
dt =
3×1 datetime array
30-Dec-2015 15:54:30
30-Dec-2015 15:54:30
30-Dec-2015 15:54:30
>> d = dateshift(dt,'start','day')
d =
3×1 datetime array
30-Dec-2015 00:00:00
30-Dec-2015 00:00:00
30-Dec-2015 00:00:00
>> t = timeofday(dt)
t =
3×1 duration array
15:54:30
15:54:30
15:54:30
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!