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!