-1

How can I get the date from "30-4-2013 16:36:58" data with datastr or other function in matlab?

It gives me strange results, because the date is before than month.

yt = datestr('"30-4-2013 16:36:58"','dd-mm-yy')

yt = 04-10-35

The problem is I have bunch of data <112577x1 cell> and the raw data is like that: "4-2-2014 16:36:58" "4-2-2014 16:37:03" "4-2-2014 16:37:08"

2
  • 1
    Did you try mm-dd-yyyy? link to reference Feb 17, 2014 at 10:47
  • I gives strange results also. yt = datestr('"30-4-2013 16:36:58"','mm-dd-yy') yt = 10-04-35 datestr understands the first data as month, I have to change into day, how? Feb 17, 2014 at 10:50

3 Answers 3

3

By writing only 30-4-2013, MATLAB has no idea whether this is String or date? First convert it into the serial date number and then only convert it to date as bellow

>> datestr(datenum('30-4-2013 16:36:58','dd-mm-yy'))

This will give the result as

>> 30-Apr-2013
1
  • The problem is I have bunch of data <112577x1 cell> and the raw data is that:"4-2-2014 16:36:58" "4-2-2014 16:37:03" Feb 17, 2014 at 13:19
0

With a cell array, a variant on Bibek Subedi's answer will work:

out = datestr(datenum(C,'"dd-mm-yy HH:MM:ss"'),'dd-mm-yy');

You can change the second format string if you need some other output. Incidentally if you're getting that cell array by reading in a text file with something like textscan you should be able to read it in without the " by adding them as delimiters and treating multiple delimiters as one.

0

Matlab syntax gets date in the american notation, which is: mm-dd-yyyy. You get gibberish if you write dd-mm-yyyy, as in 30-04-2013. Writing 30/Apr/2013 or 04-30-2013 fixes the problem and gives correct answers.

1
  • The problem is I have bunch of data <112577x1 cell> and the raw data is that:"4-2-2014 16:36:58" "4-2-2014 16:37:03" Feb 17, 2014 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.