Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

Topic: Pull out specific numbers from unstructured text file
Replies: 12   Last Post: Feb 11, 2013 10:26 AM

Advanced Search

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

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
.
.
.
end

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'.

Questions:
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]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.