I've got an array that has 3 columns. Column 1 is a counter and I don't care about it, column 2 has the date in 'dd/mm/yy' format and column 3 has the time in 'HH:MM' format. I want to combine comlumn 2 and 3 so I can convert them to a serial format.
This is my code:
day = import1.textdata(2:end,2);
time = import1.textdata(2:end,3);
ns = arrayfun(@(n) datenum([day(n) time(n)],'dd/mm/yy HH:MM'),1:length(day),'UniformOutput', 0);
I am getting this error:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
how can I resolve this? Is there an easier way to do this?
Thanks!

1 件のコメント

sivakumar dumpala
sivakumar dumpala 2016 年 5 月 14 日
just concatenate those two columns.
time=[day,time]

サインインしてコメントする。

 採用された回答

Jan
Jan 2012 年 3 月 28 日

3 投票

I guess, that your day and time are cell strings. If so, you can calculate the integer and fractional parts separately and add them:
ns = datenum(day) + datenum(time);
I cannot test this currently, perhaps you have to define the format:
ns = datenum(day, 'dd/mm/yy') + datenum(time, 'HH:MM') - datenum('00:00','HH:MM');
[EDITED]: Matt found out, that datenum uses the current year for the HH:MM format.

2 件のコメント

Matt Tearle
Matt Tearle 2012 年 3 月 28 日
You can't get a date number from just HH:MM, though. Trying it, I get the appropriate time on Jan 1, 2012. I don't know how/why it defaults to that date -- it's probably deep in the documentation somewhere. So I suppose you could do:
datenum(day,'dd/mm/yy') + datenum(time, 'HH:MM') - datenum('00:00','HH:MM')
Jan
Jan 2012 年 3 月 28 日
Phew, another time where the very smart date function are too smart for me. Thanks Matt.

サインインしてコメントする。

その他の回答 (2 件)

Matt Tearle
Matt Tearle 2012 年 3 月 28 日

1 投票

What class are day and time? If they are char arrays, then:
ns = datenum(strcat(day,32,time));
If they are cell arrays, then
ns = datenum(strcat(day,{' '},time));
EDIT TO ADD: As Jan points out, you don't even need the space if you specify the date format yourself, so:
ns = datenum(strcat(day,time),'dd/mm/yyHH:MM');
works for cells or chars.

2 件のコメント

Jan
Jan 2012 年 3 月 28 日
You even do not need the space {' '}, when the format is defined accordingly as 'dd/mm/yyHH:MM'.
Matt Tearle
Matt Tearle 2012 年 3 月 28 日
Cunning!

サインインしてコメントする。

C.J. Harris
C.J. Harris 2012 年 3 月 28 日

0 投票

Be careful when working with arrays in this manner. The terms day(n) and time(n) probably don't contain what you are expecting them to. Depending on what format your time and day array are in this might be a possible solution:
day = ['01/05/12'; '01/05/12'; '01/05/12'; '01/05/12'];
time = ['23:12'; '23:13'; '23:14'; '23:15'];
ns = arrayfun(@(n) datenum([day(n,:),' ',time(n,:)],'dd/mm/yy HH:MM'),1:size(day,1),'UniformOutput', 0);

3 件のコメント

Trader
Trader 2012 年 3 月 28 日
Chris - thanks for the reply. I still get the same error, so are you suggesting that there may be special characters in my day or time values?
I am importing the values using importdata() and both date and time come in as textdata.
I've found a solution using but I'd like to use the best code (if the best code isn't what just gets the job done, haha):
t_merge = strcat(day(:),{' '}, time(:));
test = datenum(t_merge, 'dd/mm/yy HH:MM');
C.J. Harris
C.J. Harris 2012 年 3 月 28 日
My point was that if you are using 'arrayfun' then your dates and times are probably stored as an array. Indexing into the 'day' and 'time' array in the manner showed in your question will therefore only give you one character. That's why I changed the indexing and added a space in the solution I suggested.
Matt Tearle
Matt Tearle 2012 年 3 月 28 日
And if they are char arrays, then Chris's point about indexing also applies to what you tried with day(:). Linear indexing always goes down the columns of an array, because MATLAB stores data that way. Hence, if day is the char array that Chris gave in his answer, then day(:) gives '00001111////00005555...' (ie taking the characters one at a time, down each column in turn).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTime Series Objects についてさらに検索

質問済み:

2012 年 3 月 28 日

コメント済み:

2016 年 5 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by