The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Remove text from file of numerics
Replies: 4   Last Post: Aug 1, 2013 2:30 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 9,850
Registered: 6/7/07
Re: Remove text from file of numerics
Posted: Aug 1, 2013 2:30 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 8/1/2013 2:09 AM, Phil Roberts wrote:
> dpb <> wrote in message <ktbcad$qr0$>...

>> 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), ...
>> 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... :(


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.