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: 63
Registered: 6/18/12
Re: Pull out specific numbers from unstructured text file
Posted: Feb 9, 2013 2:12 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

^^^^^Okay I think I don't understand Lines 5,7,8 in your shortcut code:

Line 1: > fid=fopen(....,'rt');
Line 2: > l=' ';
Line 3: > while 1
Line 4: > l=fgetl(fid);
Line 5: > if strfind(l,'Nmoves')>0,break,end
Line 6: > end
Line 7: > Nmoves=sscanf(l,'Nmoves=%d');
Line 8: > Nrequired=fscanf(fid,'Nrequired=%d');
Line 9: > fid=fclose(fid);

My explanation is:

while 1

This is for lines 4-6 and this reads the file. If fgetl encounters the end-of-file indicator, it returns -1. So, as long as it returns 1 (i.e. anywhere before the end of the file), this statement is saying the while loop should perform the actions inside the if statement.

My explanation for line 5:
If 'Nmoves' is found in the string l (where l is the contents of the file that have been read up to that point) then stop reading at that line.

My explanations for lines 7 and 8:
7: Scan l for 'Nmoves=%d'.
8. Scan fid for 'Nrequired=%d'.

In line 8, why did you change from l to fid?
What is the connection between line 5 and lines 7,8?
How does it know, after line 5 (i.e. after reaching the end of the line containing Nmoves), that it needs to search for the next two lines?

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.