Another way is to use datestr
and datenum
to enforce the extra zeroes for conversion.
type_1 = datestr(datenum(type_1, 'dd.mm.yyyy'), 'dd.mm.yyyy');
type_2 = datestr(datenum(type_2, 'dd.mm.yyyy'), 'dd.mm.yyyy');
datestr
requires a MATLAB date number to be able to output a string representation of a date. That's why datenum
is used and you can see that even though we specify two digits for the month or day, it's smart enough to sense that if there is one digit for these quantities and the date number is successfully converted. We'd then use these date numbers to pipe into datestr
to ensure that we have extra zeroes when we need them.
However, this will convert the cell arrays into character arrays. You'll have to put these back into cell arrays by using cellstr
:
type_1 = cellstr(type_1);
type_2 = cellstr(type_2);
Here's some sample output:
>> type_1 = {'23.2.2005', '23.4.2015', '5.1.2015'}
type_1 =
'23.2.2005' '23.4.2015' '5.1.2015'
>> type_2 = {'23.02.2005', '23.04.2015', '05.01.2015'}
type_2 =
'23.02.2005' '23.04.2015' '05.01.2015'
>> type_1 = datestr(datenum(type_1, 'dd.mm.yyyy'), 'dd.mm.yyyy')
type_1 =
23.02.2005
23.04.2015
05.01.2015
>> type_2 = datestr(datenum(type_2, 'dd.mm.yyyy'), 'dd.mm.yyyy')
type_2 =
23.02.2005
23.04.2015
05.01.2015
>> type_1 = cellstr(type_1)
type_1 =
'23.02.2005'
'23.04.2015'
'05.01.2015'
>> type_2 = cellstr(type_2)
type_2 =
'23.02.2005'
'23.04.2015'
'05.01.2015'
>> ismember(type_1, type_2)
ans =
1
1
1