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
0 件のコメント
採用された回答
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
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 Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!