1

I have a cell array of dates as strings (format: 'mm/dd/yyyy'), where the string length varies by cell entry from 8 to 10, depending on the date (e.g., n = 8 for '1/1/2015' and n = 10 for '10/10/2015'). I want to convert each cell array entry to its corresponding double as a datenum quantity. I've tried:

id = find(~cellfun( @isempty, regexp( dateList, '/', 'tokenExtents' ) ) );

and

id = find(~cellfun( @isempty, strfind( dateList, '/' ) ) );

but this isn't right. A snippet of the cell array is provided:

dateList = {'9/9/2016';
            '9/10/2016';
            '10/10/2016';
            '10/11/2016'};
1
  • You may have a specific reason for using datenum, but if not, then you should move towards using the more modern datetime functionality. Commented Nov 7, 2017 at 17:05

2 Answers 2

3

All you have to use is datenum, since it will accept a cell array of strings:

dateList = {'9/9/2016'; '9/10/2016'; '10/10/2016'; '10/11/2016'};
id = datenum(dateList);

And to confirm it worked:

>> datestr(id)

ans =

09-Sep-2016
10-Sep-2016
10-Oct-2016
11-Oct-2016
Sign up to request clarification or add additional context in comments.

Comments

0

1) Extract each cell to 3 different blocks with strsplit: day,month and year

2) Use pad to make each block the same length (2 for day and month and 4 for year)

3) Join the string together with [D,'/',M,'/',Y] and you are able to run datenum without problem.

Edit 1: I think the other answer is far more easy, I didn't know datenum can take strings with slightly different foramt.

1 Comment

Thanks. The first reply works just fine; I was making it too hard, but I didn't know that datenum could accept a cell array of strings.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.