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.

有帮助吗?

解决方案

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

其他提示

DateString = '19-May-2001';
formatIn = 'dd-mmm-yyyy';
datenum(DateString,formatIn)

Taken from:

http://www.mathworks.com/help/matlab/ref/datenum.html

Converting to the 'DD-MM-YYYY' string should be straightforward:

currentLine = cellArray{y,x};
DateString = [currentLine[3:4] '-' currentLine[5:6] '-20' currentLine[1:2]];

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top