divide cell array into date and time columns

조회 수: 2 (최근 30일)
AA
AA 2017년 10월 7일
답변: Peter Perkins 2017년 10월 13일
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

채택된 답변

Walter Roberson
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
AA
AA 2017년 10월 7일
I want to display the time column as well but it i get the error message: index exceeds matrix dimensions. I do not know what the problem is. My aim is the convert the time as minutes after midnight with the following formula:
>> [60,1]*sscanf( '12:48', '%2d:%2d')
ans =
768
Walter Roberson
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
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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by