Stan
Posts:
52
Registered:
6/18/12
|
|
Re: Pull out specific numbers from unstructured text file
Posted:
Feb 9, 2013 2:12 PM
|
|
^^^^^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?
|
|