Can I search a text file for a particluar word before recording data?

2 views (last 30 days)
Hi,
The data output format of the program I am using is giving me some trouble when I try to import the data into MATLAB. I need to search the file for a specific word before MATLAB starts recording the data, and I can't see how to make fscan, textscan, or textread do this.
For example, below I have included two sections of a data output file (each file contains at least 100 sections, some contain almost 1000 sections). I wish to ignore everything except the "7760" located near the end of each section (on the lines that end with "Target"). My problem is that the text that surrounds the "7760" has a varrying length and size throught the file, so I can't tell MATLAB to directly skip it. Is there a way to search the file for "BACKSTOP" and then record the "7760" and then start looking for "BACKSTOP" again?
Thanks for your help!
- {CONSIDER ALL
- PLOT FACETS OVERLAY '1-5 Winston Cone: X=(0/10), Radius=0.5'
Window Vertical: Y = -100.000 to 100.000 ( 200.000 )
Horizontal: Z = -102.100 to 102.100 ( 204.200 )
- GRID ELLIPTIC Z -25 (0/10)-0.5 (0/10)+0.5 -2@0.5 100 100
- ROTATE Y -ATAN[(0/10)/25]
- SOURCE DIRECTION -(0/10) 0 25
Source Z angle for rays
0.000 1 -7860
- TRACE
- CONSIDER ONLY FRONTSTOP BACKSTOP TARGET
- STATS ALL }
Object Rays Flux
4 100 0.000000 FRONTSTOP
5 0 0.000000 BACKSTOP
6 7760 0.7860000 Target
-----------------------------
TOTAL 7860 0.7860000
- {CONSIDER ALL
- PLOT FACETS OVERLAY '1-5 Winston Cone: X=(1/10), Radius=0.5'
Window Vertical: Y = -100.000 to 100.000 ( 200.000 )
Horizontal: Z = -102.100 to 102.100 ( 204.200 )
- GRID ELLIPTIC Z -25 (1/10)-0.5 (1/10)+0.5 -2@0.5 100 100
7860 rays created in a ELLIPTICAL grid for a total of 7860
- ROTATE Y -ATAN[(1/10)/25]
- SOURCE DIRECTION -(1/10) 0 25
Source Z angle for rays
0.229 1 -7860
- TRACE
## Total of 99.61 millisec ( 93.60 millisec CPU) to trace 7,860
- CONSIDER ONLY FRONTSTOP BACKSTOP TARGET
- STATS ALL }
Object Rays Flux
4 0 0.000000 FRONTSTOP
5 100 0.000000 BACKSTOP
6 7760 0.7860000 Target
-----------------------------
TOTAL 7860 0.7860000

Accepted Answer

Albert Yam
Albert Yam on 5 Jul 2012
Look for a specific keyword(s), not exactly sure about your example, perhaps something like this.
Keyword = 'Object Rays Flux';
while 1
line_str = fgetl(fid); % Get the next line
if line_str == -1 % -1 if fgetl fails or is at eof
error([Keyword 'not found'])
elseif strcmp(line_str, Keyword) % Keyword found, break the loop.
break;
end
end
%Next line, start collecting data here

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!