determine the limit of txt file while using regexp

1 view (last 30 days)
content = fileread( 'trial.txt' ) ;
matches = regexp( content, '[-\d\.]+(?=-\d)', 'match' ) ;
data = str2double( matches );
% With above commands, I extract the specific numeric values in the attached file. But I need to limit this search from %beginning to "START OF RMS MAP" (line 32, but this number is variable) not all lines. How can I set the this last line while I'm using regexp.
  2 Comments
Cedric
Cedric on 14 Sep 2015
Edited: Cedric on 15 Sep 2015
Stephen's approach is the best, but if you can't figure out how to adapt it to your case and need to work out a solution quickly, you can always do
content = strsplit( fileread( 'trial.txt' ), 'START OF RMS MAP' ) ;
data = str2double( regexp( content{1}, '[-\d\.]+(?=-\d)', 'match' )) ;
PS: if this question is still about extracting only the numbers mentioned in this thread, you should have made it clear initially, because Stephen wrote a complete solution for extracting all numbers within the section that you mention.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 14 Sep 2015
It is often faster and easier just to read in all of the data from a file, then then use simple indexing to pick the bits that you need. This will read the first part of the file:
opt = {'CollectOutput',true};
fid = fopen('trial.txt','rt');
C_date = textscan(fid,'%f%f%f%f%f%f%*s',1,'HeaderLines',5,opt{:});
for k = 1:4
C_lat(k,:) = textscan(fid,'%f%f%f%f%f%*s',1,'Delimiter','- ',opt{:});
C_map(k,:) = textscan(fid,repmat('%f',1,16),5,opt{:});
end
fclose(fid);
  2 Comments
sermet
sermet on 14 Sep 2015
when I use celldisp(C_lat) to see the numbers, there are all empty arrays of C_lat have been seen. How can I convert to doubles for C_lat?
Stephen23
Stephen23 on 14 Sep 2015
Edited: Stephen23 on 14 Sep 2015
I do not understand your request. When I run that code using your file I get these outputs:
>> celldisp(C_lat)
C_lat{1} =
87.5000 180.0000 180.0000 5.0000 450.0000
C_lat{2} =
85 180 180 5 450
C_lat{3} =
82.5000 180.0000 180.0000 5.0000 450.0000
C_lat{4} =
80 180 180 5 450
And similarly for C_map:
>> celldisp(C_map)
C_map{1} =
259 260 262 263 264 265 265 266 266 266 266 266 265 264 262 261
259 257 254 252 249 245 242 239 235 231 228 224 221 217 214 211
208 206 203 201 200 198 198 197 197 197 197 198 198 200 201 203
205 207 209 211 214 216 219 221 224 226 229 232 234 237 239 242
244 246 248 250 252 254 256 257 259 NaN NaN NaN NaN NaN NaN NaN
C_map{2} =
266 268 271 274 276 279 281 283 285 287 288 289 289 288 287 285
282 278 273 268 262 255 248 240 232 224 216 208 200 193 186 179
....
You can access the elements of a cell array using indexing:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!