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:
Remove text from file of numerics

Subject: Remove text from file of numerics

From: Phil Roberts

Date: 31 Jul, 2013 09:37:10

Message: 1 of 5

Hello,

I have the following data within a text file:

Cycle 1
x y z
1 10 100
2 20 200
3 30 300
4 40 400
5 50 500
6 60 600
7 70 700
8 80 800
9 90 900
10 100 1000

Cycle 2
x y z
11 110 1100
12 120 1200
13 130 1300
14 140 1400
15 150 1500
16 160 1600
17 170 1700
18 180 1800
19 190 1900
20 200 2000

Cycle 3
x y z
21 210 2100
22 220 2200
23 230 2300
24 240 2400
25 250 2500
26 260 2600
27 270 2700
28 280 2800
29 290 2900
30 300 3000

How do I read in this data so I just have a matrix of 30 x 3 without the strings?

Many thanks,

Phil

Subject: Remove text from file of numerics

From: dpb

Date: 31 Jul, 2013 16:00:10

Message: 2 of 5

On 7/31/2013 4:37 AM, Phil Roberts wrote:
> Hello,
>
> I have the following data within a text file:
>
> Cycle 1
> x y z
> 1 10 100
> 2 20 200
> 3 30 300
> 4 40 400
> 5 50 500
> 6 60 600
> 7 70 700
> 8 80 800
> 9 90 900
> 10 100 1000
>
> Cycle 2
> x y z
> 11 110 1100
> 12 120 1200
> 13 130 1300
> 14 140 1400
> 15 150 1500
> 16 160 1600
> 17 170 1700
> 18 180 1800
> 19 190 1900
> 20 200 2000
>
> Cycle 3
> x y z
> 21 210 2100
> 22 220 2200
> 23 230 2300
> 24 240 2400
> 25 250 2500
> 26 260 2600
> 27 270 2700
> 28 280 2800
> 29 290 2900
> 30 300 3000
>
> How do I read in this data so I just have a matrix of 30 x 3 without the
> strings?

doc textscan

It'll read multiple calls from the same file and since it appears the
file is counted number of lines/cycle you can use 'headerline',2 once
then 3 thereafter (unless the blank line also begins the file, then it's
even simpler as there are 3 header lines for all cases). Repeat until
feof(fid)

You'll get a cell array unfortunately, but you can the cast it to a
matrix w/ cell2mat.

Other than that, just use fgetl() to read the headerlines and then
fscanf() to return the array data in a loop again until feof()

--

Subject: Remove text from file of numerics

From: Phil Roberts

Date: 1 Aug, 2013 07:09:10

Message: 3 of 5

dpb <none@non.net> wrote in message <ktbcad$qr0$1@speranza.aioe.org>...
> On 7/31/2013 4:37 AM, Phil Roberts wrote:
> > Hello,
> >
> > I have the following data within a text file:
> >
> > Cycle 1
> > x y z
> > 1 10 100
> > 2 20 200
> > 3 30 300
> > 4 40 400
> > 5 50 500
> > 6 60 600
> > 7 70 700
> > 8 80 800
> > 9 90 900
> > 10 100 1000
> >
> > Cycle 2
> > x y z
> > 11 110 1100
> > 12 120 1200
> > 13 130 1300
> > 14 140 1400
> > 15 150 1500
> > 16 160 1600
> > 17 170 1700
> > 18 180 1800
> > 19 190 1900
> > 20 200 2000
> >
> > Cycle 3
> > x y z
> > 21 210 2100
> > 22 220 2200
> > 23 230 2300
> > 24 240 2400
> > 25 250 2500
> > 26 260 2600
> > 27 270 2700
> > 28 280 2800
> > 29 290 2900
> > 30 300 3000
> >
> > How do I read in this data so I just have a matrix of 30 x 3 without the
> > strings?
>
> doc textscan
>
> It'll read multiple calls from the same file and since it appears the
> file is counted number of lines/cycle you can use 'headerline',2 once
> then 3 thereafter (unless the blank line also begins the file, then it's
> even simpler as there are 3 header lines for all cases). Repeat until
> feof(fid)
>
> You'll get a cell array unfortunately, but you can the cast it to a
> matrix w/ cell2mat.
>
> Other than that, just use fgetl() to read the headerlines and then
> fscanf() to return the array data in a loop again until feof()
>
> --

Hello,

I am still a little unsure what you mean. I have used textscan and only the first block of data comes through; hence the script stops before reading the other two blocks of data.

I have never used feof. Please can you help with some example code?

Many thanks,

Phil

Subject: Remove text from file of numerics

From: dpb

Date: 1 Aug, 2013 13:05:06

Message: 4 of 5

On 8/1/2013 2:09 AM, Phil Roberts wrote:
> dpb <none@non.net> wrote in message <ktbcad$qr0$1@speranza.aioe.org>...
>> On 7/31/2013 4:37 AM, Phil Roberts wrote:
>> > Hello,
>> >
>> > I have the following data within a text file:
>> >
>> > Cycle 1
>> > x y z
>> > 1 10 100
>> > 2 20 200
>> > 3 30 300
>> > 4 40 400
>> > 5 50 500
>> > 6 60 600
>> > 7 70 700
>> > 8 80 800
>> > 9 90 900
>> > 10 100 1000
>> >
>> > Cycle 2
>> > x y z
>> > 11 110 1100
>> > 12 120 1200
>> > 13 130 1300
>> > 14 140 1400
>> > 15 150 1500
>> > 16 160 1600
>> > 17 170 1700
>> > 18 180 1800
>> > 19 190 1900
>> > 20 200 2000
>> >
>> > Cycle 3
>> > x y z
>> > 21 210 2100
>> > 22 220 2200
>> > 23 230 2300
>> > 24 240 2400
>> > 25 250 2500
>> > 26 260 2600
>> > 27 270 2700
>> > 28 280 2800
>> > 29 290 2900
>> > 30 300 3000
>> >
>> > How do I read in this data so I just have a matrix of 30 x 3 without
>> the
>> > strings?
>>
>> doc textscan
>>
>> It'll read multiple calls from the same file and since it appears the
>> file is counted number of lines/cycle you can use 'headerline',2 once
>> then 3 thereafter (unless the blank line also begins the file, then
>> it's even simpler as there are 3 header lines for all cases). Repeat
>> until feof(fid)
>>
>> You'll get a cell array unfortunately, but you can the cast it to a
>> matrix w/ cell2mat.
>>
>> Other than that, just use fgetl() to read the headerlines and then
>> fscanf() to return the array data in a loop again until feof()
>>
>> --
>
> Hello,
>
> I am still a little unsure what you mean. I have used textscan and only
> the first block of data comes through; hence the script stops before
> reading the other two blocks of data.
>
> I have never used feof. Please can you help with some example code?

fid=fopen('filename','r');
while ~feof(fid)
   l=fgetl(fid);
end

Unless you repeat the textscan() call for each block then you'll only
get one set, indeed, because the conversion on the numeric value will
fail when gets to the next header line.

You may have to play around some to discern just where it leaves the
record pointer to begin the subsequent scans...

--

Subject: Remove text from file of numerics

From: dpb

Date: 1 Aug, 2013 18:30:48

Message: 5 of 5

On 8/1/2013 2:09 AM, Phil Roberts wrote:
> dpb <none@non.net> wrote in message <ktbcad$qr0$1@speranza.aioe.org>...
...

>>
>> It'll read multiple calls from the same file and since it appears the
>> file is counted number of lines/cycle you can use 'headerline',2 once
>> then 3 thereafter (unless the blank line also begins the file, then
>> it's even simpler as there are 3 header lines for all cases). Repeat
>> until feof(fid)
>>
>> You'll get a cell array unfortunately, but you can the cast it to a
>> matrix w/ cell2mat.
...

>
> I am still a little unsure what you mean. I have used textscan and only
> the first block of data comes through; hence the script stops before
> reading the other two blocks of data.
>
> I have never used feof. Please can you help with some example code?
...

I did cut each section to three lines and ensured there was the blank
line also at the beginning of the file as well as between the sections...

 >> fid=fopen('cycle.txt','r');
 >> c(1)=textscan(fid,repmat('%f',1,3), ...
                             'headerlines',3, 'collectoutput',1);
 >> idx=2;
 >> while ~feof(fid)
      c(idx)=textscan(fid,repmat('%f',1,3), ...
                      'headerlines',2,'collectoutput',1);idx=idx+1;end
 >> fid=fclose(fid);
 >> c=cell2mat(c')
c =
            1 10 100
            2 20 200
            3 30 300
           11 110 1100
           12 120 1200
           13 130 1300
           21 210 2100
           22 220 2200
           23 230 2300
 >>

NB that have to treat first group differently as the scan goes until the
% f fails which reads the subsequent record. Also for convenience
assume know that there are three/row; otherwise if is unknown have to
count to find out how to reshape.

Then, since textscan() only returns a cell have to cast to the array at
the end.

Also, if the file is large will probably begin to bog down w/ the
preallocation, but those are implementation details can work out...

Oh, would the nuances of scanned i/o not be so murky...I could _NOT_ get
the counted read to work to avoid the difference between first and
subsequent groups...could never get it to reliably break at same place
between groups irregardless of how I tried it...

I'll lament again that it's a shame TMW didn't use the Fortran FORMAT
and i/o subsystem instead of C... :(

--

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