Thread Subject: How to import timestamp (5:48:05 ) to a format ML can handle

Subject: How to import timestamp (5:48:05 ) to a format ML can handle

From: AMK

Date: 6 Oct, 2009 19:56:50

Message: 1 of 5

Hi,
I have a datafile that I'm reading into ML and am stuck on how to get the timestamp into ML in a form that it will be able to handle. Here is my current import script. Any suggestions? The whole file is tab delimited but the timestamp cell has :, which I hope may be of some help here.

[code]
fnames = dir('*.ddf');
dr1 = 10; dc1 = 1; dr2 = 10; dc2 = 1; %This defines where the time is located within the .ddf file
dtcols = (dr2-dr1)+1; %Defines length of the data column
dtrows = length(fnames); %Defines the length of the data rows
ch1_dt = zeros(dtrows, dtcols); %Initializes a matrix to store the data
for n = 1:length(fnames) %Start the for loop for the file names
  ch1_dt(n,:) = dlmread(fnames(n).name, '\t', [dr1 dc1 dr2 dc2])'; %Read in the data
  end %End the loop
[/code]

Here is a sample of the '\t' delimited datafile:
date 9/30/2009
time 5:48:05

Obviously, my next question will be how to import the date too, but that's less critical.

Thanks

Subject: How to import timestamp (5:48:05 ) to a format ML can handle

From: TideMan

Date: 6 Oct, 2009 20:30:19

Message: 2 of 5

On Oct 7, 8:56 am, AMK <kennas...@gmail.com> wrote:
> Hi,
> I have a datafile that I'm reading into ML and am stuck on how to get the timestamp into ML in a form that it will be able to handle.  Here is my current import script.  Any suggestions?  The whole file is tab delimited but the timestamp cell has :, which I hope may be of some help here.
>
> [code]
> fnames = dir('*.ddf');
> dr1 = 10; dc1 = 1; dr2 = 10; dc2 = 1; %This defines where the time is located within the .ddf file
> dtcols = (dr2-dr1)+1; %Defines length of the data column
> dtrows = length(fnames);  %Defines the length of the data rows
> ch1_dt = zeros(dtrows, dtcols);    %Initializes a matrix to store the data
> for n = 1:length(fnames)    %Start the for loop for the file names
>   ch1_dt(n,:) = dlmread(fnames(n).name, '\t', [dr1 dc1 dr2 dc2])'; %Read in the data
>   end %End the loop
> [/code]
>
> Here is a sample of the '\t' delimited datafile:
> date    9/30/2009
> time    5:48:05
>
> Obviously, my next question will be how to import the date too, but that's less critical.
>
> Thanks

Matlab can handle a wide variety of date and time formats, but you
need to import them as strings (like '9/30/2009' and '5:48:05'):
help datenum
Try this:
datestr(datenum('9/30/2009 5:48:05','mm/dd/yyyy HH:MM:SS'))

Subject: How to import timestamp (5:48:05 ) to a format ML can handle

From: AMK

Date: 6 Oct, 2009 22:12:19

Message: 3 of 5

Great, however, my time and date values are nestled quite comfortably amongst the raw data file that has strings, numbers, is mostly tab delimited but also has potential delimiters in certain cells (such as the date and time). I'm not sure where to call the datenum(). My time location actually gets defined in line 2 of the code, but gets inserted from within the for loop.

Sincerely,
Still stumped

Subject: How to import timestamp (5:48:05 ) to a format ML can handle

From: AMK

Date: 6 Oct, 2009 22:39:12

Message: 4 of 5

This:

[code]
for n = 1:length(fnames) %Start the for loop for the file names
  ch4_dt(n,:) = dlmread(fnames(n).name, '\t', datenum([dr1 dc1 dr2 dc2]))'; %Read in the data
  %timestamp(n,:) = dlmread(fnames(n).name, ';', [tr1 tc1 tr2 tc2])'; %Read in the timestamps
end %End the loop
[/code]

only gets me a 5, which is the first value in the timestamp shown in the original problem.

Subject: How to import timestamp (5:48:05 ) to a format ML can handle

From: Leslie McBrayer

Date: 7 Oct, 2009 17:43:06

Message: 5 of 5


"AMK" <kennaster@gmail.com> wrote in message
news:655747749.23145.1254868782259.JavaMail.root@gallium.mathforum.org...
> This:
>
> [code]
> for n = 1:length(fnames) %Start the for loop for the file names
> ch4_dt(n,:) = dlmread(fnames(n).name, '\t', datenum([dr1 dc1 dr2 dc2]))';
> %Read in the data
> %timestamp(n,:) = dlmread(fnames(n).name, ';', [tr1 tc1 tr2 tc2])';
> %Read in the timestamps
> end %End the loop
> [/code]
>
> only gets me a 5, which is the first value in the timestamp shown in the
> original problem.

The dlmread function cannot read formatted dates and times. Consider using
textscan to read the dates/times in as strings (type "doc textscan" at the
command prompt for more info). Then you can convert the strings to numbers
using datenum.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com