Extract sentences of a specific pattern
Afficher commentaires plus anciens
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
Réponses (2)
per isakson
le 20 Juin 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 commentaire
Priya Narayanan
le 20 Juin 2012
Priya Narayanan
le 20 Juin 2012
1 commentaire
per isakson
le 20 Juin 2012
I cannot interpret this dump.
Catégories
En savoir plus sur UMTS Test and Measurement dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!