MATLAB: How to use DATENUM on a cell array of dates (specified without time information) with each date in a different format

arraycelldatenumMATLAB

When I declare a cell array of dates in different formats, I receive an error on using DATENUM. When the following code is typed:
 
date_array = {'02/06/2002 ', '2/5/2002 ', '2/4/2002 ', '1-Feb-02 ', '1/31/2002 ','1/30/2002 '};
date_num = datenum(date_array);
The following error is obtained:
  ERROR: ??? Error using ==> datenum at 174 DATENUM failed. Error using ==> dtstr2dtvecmx Failed on converting date string to date number.

Best Answer

The ability to use DATENUM on a cell array of dates having different formats is not available.
Ideally, all dates in a cell array of strings input to DATENUM should have the same format. If this is not possble,
as a workaround, consider using the CELLFUN function to operate DATENUM on each element of the cell array as given below.
For example,
date_array = {'02/06/2002', '2/5/2002', '2/4/2002', '1-Feb-02', '1/31/2002','1/30/2002'};
date_num = cellfun(@datenum, date_array);