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: Pull out specific numbers from unstructured text file
Replies: 13   Last Post: Aug 8, 2014 6:51 AM

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: Pull out specific numbers from unstructured text file
Posted: Feb 8, 2013 8:47 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 2/7/2013 11:00 PM, Stan wrote:
> It worked exactly as you said. I had to put in a leading space, but no
> other changes were needed:

>> Nmoves=sscanf(l,' Nmoves=%d');
>> Nrequired=fscanf(fid,' Nrequired=%d');

> ------------------X--------------------
> Now, there are a few other lines immediately following line lines above.
> I would like to pull out those lines similarly. The entire file is:
> Nmoves=84 (chess win)
> Nrequired=101 (chess win maximum moves requested)
> TotRL = 0.26918E+08 (total IQ (RaL) in move)
> tolamax= 0.89606 ras
> chess one= 0.25 sh
> chess two = 0.80 sh
> Results for 0.27000E-01RaL moves
> Throw dice = 0.2183E-03+- 0.46E-05 %
> Total throw = 0.2839 +- 0.17E-03 %
> So, I've added to your relevant commands as follows:
> Nmoves=sscanf(l,' Nmoves=%d');
> Nrequired=fscanf(fid,' Nrequired=%d');
> Tot_RL=fscanf(fid,' TotRL=%15f');
> tolamax=fscanf(fid,' tolamax=%11f');
> Moves=fscanf(fid,' Results for%16f');
> Throw_dice=fscanf(fid,' Throw dice = %f+-%f');
> Total_throw=fscanf(fid,' Total throw = %f +-%f');
> Yet, these don't seem to be working. Am I missing something in here, or
> is something more than this addition required to get the extra numbers
> TotRL, tolamax, number before "Moves", Throw dice and Total throw?

Yeah, the shortcut I took doesn't go to the next line after the fscanf()
call. Since it's so irregular, to get more lines that are
near-contiquous (assuming the number of blank lines is also fixed) I'd
just continue w/ the fgetl() tack instead of writing explicit number of

Nreq=sscanf(l,' Nrequired= %d');
Tot_RL=sscanf(l,' TotRL= %f');
tolamax=sscanf(l,' tolamax= %f');
Moves=sscanf(l,' Results for %f');


NB you've got to read past the blank line(s), too...

You should be able to get fscanf() (or even textscan() ) to work as well
w/ the inclusion of counting the fields and a %*[^\n] string to skip the
\n characters (be sure to use 'rt' of the fopen in Windows to ensure the
proper NL characters are recognized assuming the file also comes from
Windows) but it's simpler to just let fgetl() take care of each reading
getting to the next record imo...

Untested, salt to suit...


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.