extract a specific line in text files

1 view (last 30 days)
how are you best community :
I have about 6000 text files each of them has 11 lines as shown next :
COM: IFN/ENIT-database truth (label) file
COM: http://www.ifnenit.com
COM: IfN, TU-BS
COM: ae07_001.tif coming from pa021_0.tif
X_Y: 449 119
BDR: begin data record
LBL: ZIP:1251;AW1:ÇáÔÑÇíÚ;AW2:aaA|laB|shM|raE|aaA|yaB|ayE|;QUA:YB2;ADD:P4
CHA: 7
BLN: 82,78
TLN: 32,62
EDR: end of data record
i want to extract from the seventh line only the text
"aaA|laB|shM|raE|aaA|yaB|ayE|"
( the part i want to extract does not has the same size from file to file )and put all of them in a new array with same size of files numbers .
how can i do it
any answer will be appreciated .
thank you

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jan 2016
Use fileread() to read the file into a single string. Use regexp() to match the portions of it you want. For example
regexp(TheString, '(?<=COM:\s+)\S+(?=\s+coming)', 'match')
  3 Comments
Walter Roberson
Walter Roberson on 29 Jan 2016
project_dir = '/path/to/where/the/tru/files/are';
dinfo = dir( fullfile(project_dir, '*.tru') );
for K = 1 : length(dinfo)
thisfile = fullfile(project_dir, dinfo(K).name);
filecontent = fileread(thisfile);
imagename_cell = regexp(filecontent, '(?<=COM:\s+)\S+(?=\s+coming)', 'match');
lbl_cell = regexp(filecontent, '(?<=LBL:\s+)\S+', 'match');
lbl_parts = strsplit(lbl_cell{1}, ';');
image_names{K} = imagename_cell{1};
image_labels{K} = lbl_parts{1};
image_texts{K} = lbl_parts{3}(5:end);
% fprintf('Image "%s" had label "%s" and text "%s"\n', image_names{K}, image_labels{K}, image_texts{K});
end
The end result will be three cell arrays of strings, image_names, image_labels, and image_texts
Note: extraction of the image name relies upon the word "coming" being present after the name.
majed majed
majed majed on 31 Jan 2016
thank you a lot for helping me

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!