Search code examples
matlabtext-processingcell-array

Creating date array from cell array of strings


I have a cell array of strings the first few elements of which look like:

'140322P00024000'
'140324PR0025000'
'140325P00Q26000'

where '140322' refers to 2014-03-22 (22nd march, 2014). I want to obtain the following array from the above:

735680
735682
735683

Please note in the given array only the first 6 letters are consistent and they refer to date.


Solution

  • The first six characters can be converted with datenum directly using the format 'yymmdd':

    >> d = datenum('140322','yymmdd')
    d =
          735680
    >> datestr(d)
    ans =
    22-Mar-2014
    

    Do them all with cellfun:

    >> cellfun(@(x)datenum(x(1:6),'yymmdd'),C)
    ans =
          735680
          735682
          735683