Extract sentences of a specific pattern

1 view (last 30 days)
Priya Narayanan
Priya Narayanan on 20 Jun 2012
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
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
Priya Narayanan on 20 Jun 2012
I get this error when I put in my string (it is two dimension). What would be the best way to work aroind this
??? Error using ==> regexp
The first argument (STRING) must be a
one-dimensional array of char or cell arrays of
strings.

Sign in to comment.


Priya Narayanan
Priya Narayanan on 20 Jun 2012
The content of my .asc file is as follows and I would like to extract all the sentences of the form
"after slot 16:...." and save it in the form of an array or structure.
Thanks
dir: /home/imt/therapist/005/therapy/20110808_Mon game: random_1 time: 095733 starting slottime: 2.0 starting stiffness: 200.0 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 after slot 64: adjust slottime 1.0 side_stiffness 242.027623793 after slot 80: adjust slottime 1.0 side_stiffness 247.294544674
pm display: slotnum 81 init 0 min_dist_from_target 4 robot_power 48 jerkmag 145 dist_straight_line 9 after slot 128: adjust slottime 1.0 side_stiffness 246.411422738
pm display: slotnum 161 init 0 min_dist_from_target 4 robot_power 42 jerkmag 137 dist_straight_line 7 after slot 176: adjust slottime 1.0 side_stiffness 246.162323485 after slot 224: adjust slottime 1.0 side_stiffness 248.961077598
pm display: slotnum 241 init 1 min_dist_from_target 3 robot_power 69 jerkmag 146 dist_straight_line 9 after slot 272: adjust slottime 1.0 side_stiffness 249.723937086
pm display: slotnum 321 init 0 min_dist_from_target 3 robot_power 60 jerkmag 151 dist_straight_line 7

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!