extract a specific line in text files

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2016-1-29
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 个评论
Walter Roberson
Walter Roberson 2016-1-29
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by