Extract sentences of a specific pattern
Show older comments
I have opened an ascii file using the following code
fid=fopen('test.asc','r');
s = '';
while ~feof(fid)
line = fgetl(fid);
s = strvcat(s,line);
end
Now the file contains sentences of the following pattern:
after slot 16: adjust slottime 1.73149698229 side_stiffness 208.220358294
after slot 32: adjust slottime 1.28516669432 side_stiffness 215.966524494
after slot 48: adjust slottime 1.0 side_stiffness 241.34853159
What is the best way to extract all the sentences of this format.
Thanks
Answers (2)
per isakson
on 20 Jun 2012
Try
str = 'after slot 16: adjust slottime 1.73149698229 side_stiffness 208.220358294';
ptn = '^after slot \d+: adjust slottime [\d\.]+ side_stiffness [\d\.]+$';
ii = regexp( str, ptn, 'start' );
not( isempty( ii ) )
.
--- Cont. ---
I have coy&pasted the your three lines to cssm.asc. Try run
str = cssm(();
If that doesn't work try to modify the pattern, ptn, to match the lines in your real file.
function str = cssm
fid = fopen( 'cssm.asc', 'r' );
cac = cell(0);
ptn = '^after slot \d+: adjust slottime [\d\.]+ side_stiffness [\d\.]+$';
while ~feof( fid )
line = fgetl( fid );
if not( isempty( regexp( line, ptn, 'start' ) ) )
cac = cat( 1, cac, {line} );
end
end
fclose( fid );
str = char( cac );
end
1 Comment
Priya Narayanan
on 20 Jun 2012
Categories
Find more on UMTS Test and Measurement in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!