Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe06.iad.POSTED!7564ea0f!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
Organization: Canada Eat The Cookie Foundation
User-Agent: Thunderbird 2.0.0.16 (Windows/20080708)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to remove unwanted text from a .txt file?
References: <gbbqun$qo5$1@fred.mathworks.com> <ryeCk.36464$QF5.28064@newsfe08.iad> <gbbubd$2n7$1@fred.mathworks.com> <VGfCk.562$Cl1.66@newsfe01.iad> <gbc1vn$8k6$1@fred.mathworks.com> <gbc2m6$de7$1@fred.mathworks.com> <gbc4i6$qf5$1@fred.mathworks.com> <gbcjal$h84$1@fred.mathworks.com> <qukCk.14892$Il.5652@newsfe09.iad> <gbcn4h$ej1$1@fred.mathworks.com> <gbcnr1$kbc$1@fred.mathworks.com> <5JlCk.9617$tp1.8665@newsfe06.iad> <gbhh9l$sv9$1@fred.mathworks.com> <gbhjs6$nr8$1@fred.mathworks.com>
In-Reply-To: <gbhjs6$nr8$1@fred.mathworks.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 70
Message-ID: <w8%Ck.38641$tp1.10075@newsfe06.iad>
NNTP-Posting-Host: 24.79.146.116
X-Complaints-To: internet.abuse@sjrb.ca
X-Trace: newsfe06.iad 1222409884 24.79.146.116 (Fri, 26 Sep 2008 06:18:04 UTC)
NNTP-Posting-Date: Fri, 26 Sep 2008 06:18:04 UTC
Date: Fri, 26 Sep 2008 01:19:05 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:492163


Cy abd wrote:
> I seem to have found a reasonable solution to my problem but however I can&#8217;t
> get it to work, Will appreciate reviewing my code below please.
> Through the following code I&#8217;m hoping to get the textscan to resume.

> fid = fopen('test.txt');

fopen returns a file identifier, which is a positive integer such as 2 .
Numbers are, of course, just a form of vectors as far as Matlab is concerned.

> [C, position] = textscan(fid(position+1:end),'%f %f %f %f %f %f ','delimiter',',');

You do not show any initialization for position. Presuming you initialized it to 0,
then you would be indexing the vector containing the 2 at positions 1 through
the end of the vector. The end of the vector is position one, so that would be 
indexing the vector containing the 2 at positions 1 through 1, which is just
going to be the scalar 2. So provided you initialized position to 0, the first
time around, the integer file identifier is going to be passed as the first
element of the textscan() call, resulting in data being read. And then the
position variable is going to be overwritten with the position that was reached in
the file. That might be, for example, 103

As you did not specify any repeat count for textscan, it is going to default
to reading as much as it can, stopping at an error or at end of file.

You do not show any kind of loop, but we can infer one based upon the fact that
you expect the code to resume reading. It won't resume reading just on a single
textscan call so you have presumably branched back up in code not shown.

The code you do show has C as the output for each textscan call, and
has C as the variable at the end that is expected to hold all of the values.
Unfortunately, the next textscan() call would overwrite C. So you are going to need
another variable, such as AllC, initialized to {}, and at each textscan() call
you are going to have to test C to see whether you got any data, and if you
did get data then AllC(end+1:end+length(C)) = C; and it would be AllC that you
would convert to an array at the end.

The second time through the loop, you would be attempting to index the vector
containing the file identifier at location 'position' through to the end. In our
earlier example we said that position might be (say) 103, so you would be attempting
to index the scalar vector containing 2 at locations 103+1 to the end of the vector.
There is, however, no location 104 in the single-element vector, so the indexing
would fail before the textscan() routine was actually called the second time.

If you were to replace the fid(position+1:end) with just fid so as to get rid
of this obvious indexing error, then textscan() would start the new scan from
the current file position. Unfortunately, the current file position would be
at the non-numeric character in the text: when the first textscan() call read
the non-numeric character and found that it did not match the numeric format 
it wanted, textscan() automatically put that non-matching character "back in the
buffer", ready to be read by the next call. And since the next call would be
attempting a numeric format, it too would fail... What you do to fix this is
to determine, after the textscan() call, whether you are at end of file;
if you are not at end of file, then fgetl(fid) to read the non-matching line.
Throw that line away (for your purposes) and loop back to try again. When do
you stop trying textscan() calls? When you detect you are at end of file via feof().

> % % % % % fid = fclose(fid);
> C = [C{:}];


Please next time read the documentation more carefully. The code you show,
textscan(fid(position+1:end),...) is obviously just a small variation on the
sample line shown in the textscan documentation talking about resuming, which has
textscan(str(position+1:end),...)
However, if you had read slightly more carefully you would have seen that
that only applies when you are using textscan() to scan a string, not when
you are using textscan to scan a file. It should have been obvious to you that
indexing the file identifier would never work. Instead, I had to write this
long posting describing all the things wrong with your code :(