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:
reading date_time in PM/AM format from an ascii file

Subject: reading date_time in PM/AM format from an ascii file

From: Maria Prokopenko

Date: 29 Jun, 2008 04:41:01

Message: 1 of 4

Hello,
i am trying to read into Matlab ascii files (part of the
first line is given here as an example, then the same string
repeats 7 more times, 8 total, in a each row of each file)

4/7/2008 3:02:42 AM 0.245 2.924686e-009

I need to convert the dates into numerical form, so i can
time-average the data etc.

I am trying to use textscan to read the am/pm format of the
dates, and while this does read 'AM' or 'PM' as a string i
cannot convert the whole string back into a date_time
format. that's what i am doing:

eims = [];
 E = dir('EIMS_ascii_formatted');
 cd EIMS_ascii_formatted\
for i =3:length(E)
    eims_file_name = E(i).name;
    disp(eims_file_name)
    fid = fopen(eims_file_name);
   
    eimst = textscan(fid,['%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32
%d/%d/%d %d:%d:%d %s %f32 %f32'],[72 inf]');
 
   eims_dates(:,1)=eimst{1};
   eims_dates(:,2)=eimst{2};
   eims_dates(:,3)=eimst{3};
   eims_dates(:,4)=eimst{4};
   eims_dates(:,5)=eimst{5};
   eims_dates(:,6)=eimst{6};
   eims_dates(:,7)=eimst{7}; (% here the problem comes: AM
or PM is not read%)

% i also tried%

eims_dates=eimst(:,1:7); % this did not work either%

   fclose(fid);
   end
  eims_dates = datenum(eims(:,1:7))


My code does not work. Any help will be very much appreciated,

Masha

Subject: reading date_time in PM/AM format from an ascii file

From: Georgios

Date: 29 Jun, 2008 14:12:01

Message: 2 of 4

"Maria Prokopenko" <masha.prokopenko@gmail.com> wrote in
message <g473ot$er2$1@fred.mathworks.com>...
> Hello,
> i am trying to read into Matlab ascii files (part of the
> first line is given here as an example, then the same string
> repeats 7 more times, 8 total, in a each row of each file)
>
> 4/7/2008 3:02:42 AM 0.245 2.924686e-009
>
> I need to convert the dates into numerical form, so i can
> time-average the data etc.
>
> I am trying to use textscan to read the am/pm format of the
> dates, and while this does read 'AM' or 'PM' as a string i
> cannot convert the whole string back into a date_time
> format. that's what i am doing:
>
> eims = [];
> E = dir('EIMS_ascii_formatted');
> cd EIMS_ascii_formatted\
> for i =3:length(E)
> eims_file_name = E(i).name;
> disp(eims_file_name)
> fid = fopen(eims_file_name);
>
> eimst = textscan(fid,['%d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32
> %d/%d/%d %d:%d:%d %s %f32 %f32'],[72 inf]');
>
> eims_dates(:,1)=eimst{1};
> eims_dates(:,2)=eimst{2};
> eims_dates(:,3)=eimst{3};
> eims_dates(:,4)=eimst{4};
> eims_dates(:,5)=eimst{5};
> eims_dates(:,6)=eimst{6};
> eims_dates(:,7)=eimst{7}; (% here the problem comes: AM
> or PM is not read%)
>
> % i also tried%
>
> eims_dates=eimst(:,1:7); % this did not work either%
>
> fclose(fid);
> end
> eims_dates = datenum(eims(:,1:7))
>
>
> My code does not work. Any help will be very much appreciated,
>
> Masha

The datestr command might be helpful. I have not tried it
with your code example, because I am not at work, where I
run Matlab. There are examples of use at the Mathworks help
desk.

>>help datestr

or

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/datestr.html&http://www.mathworks.com/cgi-bin/texis/webinator/search/?db=MSS&prox=page&rorder=750&rprox=750&rdfreq=500&rwfreq=500&rlead=250&sufs=0&order=r&is_summary_on=1&ResultCount=10&query=datestr&submitButtonName=Search

Regards,
Georgios

Subject: reading date_time in PM/AM format from an ascii file

From: Andres

Date: 30 Jun, 2008 10:54:01

Message: 3 of 4

"Maria Prokopenko" <masha.prokopenko@gmail.com> wrote in
message <g473ot$er2$1@fred.mathworks.com>...
> Hello,
> i am trying to read into Matlab ascii files (part of the
> first line is given here as an example, then the same
string
> repeats 7 more times, 8 total, in a each row of each file)
>
> 4/7/2008 3:02:42 AM 0.245 2.924686e-009
>
> I need to convert the dates into numerical form, so i can
> time-average the data etc.
> [..]



Hi Maria,

there's an easy solution - among others - with txt2mat from
the file exchange:

filename = 'c:\myFile.txt';

A = txt2mat(filename,'ReplaceExpr', ...
    {{'AM','00'}, {'PM','12'}});

%{
A contains separate columns for day, month, year...
%}

dateVector = [A(:,3), A(:,2), A(:,1), A(:,4)+ A(:,7), ...
              A(:,5), A(:,6)];

%{
This is the 'Date Vector' format you can find in the help
of datenum and other date related functions. A(:,7)
contains the additional hour number from "AM" or "PM" (0 or
12, resp.), that ist added to A(:,4). Note that I assumed
the month number to be at the second position in your file,
otherwise swap A(:,2) and A(:,1) in the dateVector
definition.
%}

% Now you can use matlab's date functions, e.g.
n = datenum(dateVector);


I checked this on a file containing the following lines:
4/7/2008 3:02:42 AM 0.245 2.924686e-009
4/7/2008 3:02:42 PM 0.245 2.924686e-009
5/7/2008 3:02:42 PM 0.245 2.924686e-009
6/7/2008 3:02:42 AM 0.245 2.924686e-009


Hth, regards
Andres

Subject: reading date_time in PM/AM format from an ascii file

From: Maria Prokopenko

Date: 30 Jun, 2008 13:28:01

Message: 4 of 4

"Andres " <rantore@werb.de> wrote in message
<g4ae09$s37$1@fred.mathworks.com>...
> "Maria Prokopenko" <masha.prokopenko@gmail.com> wrote in
> message <g473ot$er2$1@fred.mathworks.com>...
> > Hello,
> > i am trying to read into Matlab ascii files (part of the
> > first line is given here as an example, then the same
> string
> > repeats 7 more times, 8 total, in a each row of each file)
> >
> > 4/7/2008 3:02:42 AM 0.245 2.924686e-009
> >
> > I need to convert the dates into numerical form, so i can
> > time-average the data etc.
> > [..]
>
>


Thank you very much for your help! i am trying it out right now.

masha
> Hi Maria,
>
> there's an easy solution - among others - with txt2mat from
> the file exchange:
>
> filename = 'c:\myFile.txt';
>
> A = txt2mat(filename,'ReplaceExpr', ...
> {{'AM','00'}, {'PM','12'}});
>
> %{
> A contains separate columns for day, month, year...
> %}
>
> dateVector = [A(:,3), A(:,2), A(:,1), A(:,4)+ A(:,7), ...
> A(:,5), A(:,6)];
>
> %{
> This is the 'Date Vector' format you can find in the help
> of datenum and other date related functions. A(:,7)
> contains the additional hour number from "AM" or "PM" (0 or
> 12, resp.), that ist added to A(:,4). Note that I assumed
> the month number to be at the second position in your file,
> otherwise swap A(:,2) and A(:,1) in the dateVector
> definition.
> %}
>
> % Now you can use matlab's date functions, e.g.
> n = datenum(dateVector);
>
>
> I checked this on a file containing the following lines:
> 4/7/2008 3:02:42 AM 0.245 2.924686e-009
> 4/7/2008 3:02:42 PM 0.245 2.924686e-009
> 5/7/2008 3:02:42 PM 0.245 2.924686e-009
> 6/7/2008 3:02:42 AM 0.245 2.924686e-009
>
>
> Hth, regards
> Andres

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