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:
Read txt file with different elements format

Subject: Read txt file with different elements format

From: Danuli Gonzalez

Date: 16 Aug, 2011 16:42:28

Message: 1 of 4

Dear all,

I am trying to read into three variables a txt file with the following format:

 000002456 03 May 2010 00:00:00.045 03 May 2010 01:11:33.000
 000000098 03 May 2010 00:00:00.078 03 May 2010 01:14:33.000
.......

And so on, with a very large number of rows.

I would like to save the data in three variables:

1) Vector containing the first element of each row:

000002456
000000098
.....


2) vector containing the 1st time stamp:

03 May 2010 00:00:00.045
03 May 2010 00:00:00.078
.....


3) vector containing the 2nd time stamp

03 May 2010 01:11:33.000
03 May 2010 01:14:33.000
....


I am using fscanf like this:

f1 = fopen('test.txt');
a = fscanf(f1, '%u %s %s')

But I am obtaining just a coulumn of numbers, for example:

 2456
          48
          51
          77
          97
         121
        2010
          48
          48
          58
          48
          48
          58
          48
          48
          46
          48
          52
          53
          48
          51


Could you help me with this?


Thanks a lot!

Subject: Read txt file with different elements format

From: Miroslav Balda

Date: 16 Aug, 2011 19:20:31

Message: 2 of 4

"Danuli Gonzalez" wrote in message <j2e6lk$on0$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I am trying to read into three variables a txt file with the following format:
>
> 000002456 03 May 2010 00:00:00.045 03 May 2010 01:11:33.000
> 000000098 03 May 2010 00:00:00.078 03 May 2010 01:14:33.000
> .......
>
> And so on, with a very large number of rows.
>
> I would like to save the data in three variables:
>
> 1) Vector containing the first element of each row:
>
> 000002456
> 000000098
> .....
>
>
> 2) vector containing the 1st time stamp:
>
> 03 May 2010 00:00:00.045
> 03 May 2010 00:00:00.078
> .....
>
>
> 3) vector containing the 2nd time stamp
>
> 03 May 2010 01:11:33.000
> 03 May 2010 01:14:33.000
> ....
>
>
> I am using fscanf like this:
>
> f1 = fopen('test.txt');
> a = fscanf(f1, '%u %s %s')
>
> But I am obtaining just a coulumn of numbers, for example:
>
> 2456
> 48
> 51
> 77
> 97
> 121
> 2010
> 48
> 48
> 58
> 48
> 48
> 58
> 48
> 48
> 46
> 48
> 52
> 53
> 48
> 51
>
>
> Could you help me with this?
>
>
> Thanks a lot!

Hi
Read help textscan
Mira

Subject: Read txt file with different elements format

From: dpb

Date: 16 Aug, 2011 19:23:51

Message: 3 of 4

On 8/16/2011 11:42 AM, Danuli Gonzalez wrote:
...

> I am trying to read into three variables a txt file with the following
> format:
>
> 000002456 03 May 2010 00:00:00.045 03 May 2010 01:11:33.000
> 000000098 03 May 2010 00:00:00.078 03 May 2010 01:14:33.000
> .......
>
...
> I would like to save the data in three variables:
>
> 1) Vector containing the first element of each row:
...
> 2) vector containing the 1st time stamp:
> .....
> 3) vector containing the 2nd time stamp
> ....
>
>
> I am using fscanf like this:
>
> f1 = fopen('test.txt');
> a = fscanf(f1, '%u %s %s')
>
...

 From

doc fscanf
...
> Mixing character and numeric conversion specifications will cause
> the resulting matrix to be numeric and any characters read to show
> up as their ASCII values one character per MATLAB matrix element.

Your best bet would be to have the file(s) written as other than
blank-delimited in which case you could use textscan w/ the 'delimiter'
option to separate the fields.

W/ the blank-delimited fields, your choices will be to either read a
line and parse it w/ regexp expression or to read the various fields w/
and reconstitute them into a string column for the dates. I'm presuming
that when you get past May into April the field length is going to
change so even a count for field width wouldn't help w/o the delimiter.

--

Subject: Read txt file with different elements format

From: Barry Williams

Date: 16 Aug, 2011 19:33:12

Message: 4 of 4

"Danuli Gonzalez" wrote in message <j2e6lk$on0$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I am trying to read into three variables a txt file with the following format:
>
> 000002456 03 May 2010 00:00:00.045 03 May 2010 01:11:33.000
> 000000098 03 May 2010 00:00:00.078 03 May 2010 01:14:33.000
> .......
>
> And so on, with a very large number of rows.
>
> I would like to save the data in three variables:
>
> 1) Vector containing the first element of each row:
>
> 000002456
> 000000098
> .....
>
>
> 2) vector containing the 1st time stamp:
>
> 03 May 2010 00:00:00.045
> 03 May 2010 00:00:00.078
> .....
>
>
> 3) vector containing the 2nd time stamp
>
> 03 May 2010 01:11:33.000
> 03 May 2010 01:14:33.000
> ....
>
>
> I am using fscanf like this:
>
> f1 = fopen('test.txt');
> a = fscanf(f1, '%u %s %s')
>
> But I am obtaining just a coulumn of numbers, for example:
>
> 2456
> 48
> 51
> 77
> 97
> 121
> 2010
> 48
> 48
> 58
> 48
> 48
> 58
> 48
> 48
> 46
> 48
> 52
> 53
> 48
> 51
>
>
> Could you help me with this?
>
>
> Thanks a lot!

Someone may come along with a better approach, but in the meantime, this may give you a start. I tried some dummy data in a format like yours. You could use textscan looping through several times to read the data. Use the skip function the * symbol to skip the fields you don't need in each loop. Anyway it worked for me when I tried it.
Barry

Tags for this Thread

No tags are associated with 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