Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

creating the same format for the dates

Asked by tzaloupas on 1 Feb 2013

Dear all,

I have this date issue

dates= {

    '11/2008'
    '12/2008'
    '1/02/2010'
    '2/02/2010'
     '12/03/2010'
    }

where 11/2008 is mm/yyyy As you can see the format of the dates is not the same

I would like to have either the format dd/mm/yyyy or the format mm/yy

Is there any way of doing that in matlab?

The full date vector is 40000 by 1 thanks

0 Comments

tzaloupas

Tags

Products

No products are associated with this question.

1 Answer

Answer by Miroslav Balda on 2 Feb 2013
Accepted answer

The following code fulfils the requirement, should the format of input dates be dd/mm/yyyy or mm/yyyy:

dates= {
    '11/2008'
    '12/2008'
    '1/02/2010'
    '2/02/2010'
     '12/03/2010'
    }
for k=1:length(dates)
%    I = find(cell2mat(d(k))=='/');
    I = find(dates{k}=='/');
    if length(I)==1
        d = ['22/' dates{k}];
    else
        d = dates{k};
    end
    c=datevec(d,'dd/mm/yyyy');
    dates{k} = sprintf('%02d/%02d', c(2), c(1)-2000);
end
dates

0 Comments

Miroslav Balda

Contact us