Extract sentences of a specific pattern
이전 댓글 표시
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
답변 (2개)
per isakson
2012년 6월 20일
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
카테고리
도움말 센터 및 File Exchange에서 UMTS Test and Measurement에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!