Got Questions? Get Answers.
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

Thread Subject:
Help with datenum and datestr

Subject: Help with datenum and datestr

From: naseeb

Date: 13 Jul, 2010 22:59:05

Message: 1 of 4

Hi there,

I need to convert a date from Excel file to be used in Matlab. In Excel file it is, '14/03/2009 12:49:59 PM'

I want to convert it to '14-March-2009 12:49:59 PM' in Matlab.

I tried as,
datestr(datenum('14/03/2009 12:49:59 PM','dd/mm/yyyy HH:MM:SS PM'))
it returns, '14-Mar-2009 12:49:59' without AM/PM :((

Can anybody help me i how to do it?

Thanks
Adnan

Subject: Help with datenum and datestr

From: Nathan

Date: 13 Jul, 2010 23:20:43

Message: 2 of 4

On Jul 13, 3:59 pm, "naseeb " <adnan....@gmail.com> wrote:
> Hi there,
>
> I need to convert a date from Excel file to be used in Matlab. In Excel file it is, '14/03/2009 12:49:59 PM'
>
> I want to convert it to '14-March-2009 12:49:59 PM' in Matlab.
>
> I tried as,
> datestr(datenum('14/03/2009 12:49:59 PM','dd/mm/yyyy HH:MM:SS PM'))
> it returns, '14-Mar-2009 12:49:59' without AM/PM :((
>
> Can anybody help me i how to do it?
>
> Thanks
> Adnan

Why bother converting it to a datenum and back to a datestr?
Just replace the /'s with -'s

Ex:

a = '14/03/2009 12:49:59 PM';
a(a=='/') = '-'
%%%%%%%%%%%%%%%
a =
14-03-2009 12:49:59 PM


-Nathan

Subject: Help with datenum and datestr

From: Nathan

Date: 13 Jul, 2010 23:26:31

Message: 3 of 4

On Jul 13, 4:20 pm, Nathan <ngrec...@gmail.com> wrote:
> On Jul 13, 3:59 pm, "naseeb " <adnan....@gmail.com> wrote:
>
> > Hi there,
>
> > I need to convert a date from Excel file to be used in Matlab. In Excel file it is, '14/03/2009 12:49:59 PM'
>
> > I want to convert it to '14-March-2009 12:49:59 PM' in Matlab.
>
> > I tried as,
> > datestr(datenum('14/03/2009 12:49:59 PM','dd/mm/yyyy HH:MM:SS PM'))
> > it returns, '14-Mar-2009 12:49:59' without AM/PM :((
>
> > Can anybody help me i how to do it?
>
> > Thanks
> > Adnan
>
> Why bother converting it to a datenum and back to a datestr?
> Just replace the /'s with -'s
>
> Ex:
>
> a = '14/03/2009 12:49:59 PM';
> a(a=='/') = '-'
> %%%%%%%%%%%%%%%
> a =
> 14-03-2009 12:49:59 PM
>
> -Nathan

Ah, ignore that. I misread the 14-Mar-2009.

You didn't tell datestr the format you wanted:

datestr(datenum('14/03/2009 12:49:59 PM','dd/mm/yyyy HH:MM:SS PM'),'dd-
mmm-yyyy HH:MM:SS PM')
%%%%%%%%%%%%%%%%%
14-Mar-2009 12:49:59 PM


-Nathan

Subject: Help with datenum and datestr

From: per isakson

Date: 14 Jul, 2010 02:29:05

Message: 4 of 4

"naseeb " <adnan.iut@gmail.com> wrote in message <i1ir3o$jcm$1@fred.mathworks.com>...
> Hi there,
>
> I need to convert a date from Excel file to be used in Matlab. In Excel file it is, '14/03/2009 12:49:59 PM'
>
> I want to convert it to '14-March-2009 12:49:59 PM' in Matlab.
>
> I tried as,
> datestr(datenum('14/03/2009 12:49:59 PM','dd/mm/yyyy HH:MM:SS PM'))
> it returns, '14-Mar-2009 12:49:59' without AM/PM :((
>
> Can anybody help me i how to do it?
>

The default format of datestr is 'dd-mmm-yyyy HH:MM:SS' (i.e. 24h)

>> datestr(datenum('14/03/2009 12:49:59 PM','dd/mm/yyyy HH:MM:SS PM'), 'dd-mmm-yyyy HH:MM:SS AM' )
ans =
14-Mar-2009 12:49:59 PM

you have to specify the format of the output / per

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us