Find specific strings in text files

조회 수: 6 (최근 30일)
Jerone
Jerone 2015년 10월 28일
댓글: Jerone 2015년 10월 28일
I have a text file which looks like this (stored as 'images.txt'):
{{{"n00442981", 35337, "JPEG", "n00442981_35337.JPEG"}, {0.09406380813043877, 0.17861690849412887, 0.04507771067833858, 0.029303014634447882, 0.012220201853215573, 0.1257934815481311, 0.13825580156603434, 0.9389469278576577, 0.004618802153517006, 0.04270831300812525, 0.19442907875795395}}, {{"n00442981", 29039, "JPEG", "n00442981_29039.JPEG"}, {0.3070995936627196, 0.25283420197797524, 0.2906750395366373, 0.021067754208260528, 0., 0.10090499582190261, 0.19841618874784198, 0.8236737841365335, 0.05066403971048989, 0.0903942641689859, 0.13454220259113817}}, {{"n00442981", 23083, "JPEG", "n00442981_23083.JPEG"}, {0.11339606107209661, 0.2696565717105123, 0.036000000000000004, 0.002309401076758503, 0., 0.023094010767585032, 0.006110100926607786, 0.9520056022244127, 0.008640987597877146, 0.027519689920733725, 0.07353910524340095}}}
I want to extract the names of the JPEGs into a cell array, and the 11 values that represent each JPEG into a matrix (where each row is a JPEG and the columns are the 11 values).
Such that I get:
A = {'n00442981_35337', 'n00442981_29039', 'n00442981_23083'};
and
B = [0.09406380813043877, 0.17861690849412887, 0.04507771067833858, 0.029303014634447882, 0.012220201853215573, 0.1257934815481311, 0.13825580156603434, 0.9389469278576577, 0.004618802153517006, 0.04270831300812525, 0.19442907875795395; 0.3070995936627196, 0.25283420197797524, 0.2906750395366373, 0.021067754208260528, 0., 0.10090499582190261, 0.19841618874784198, 0.8236737841365335, 0.05066403971048989, 0.0903942641689859, 0.13454220259113817; 0.11339606107209661, 0.2696565717105123, 0.036000000000000004, 0.002309401076758503, 0., 0.023094010767585032, 0.006110100926607786, 0.9520056022244127, 0.008640987597877146, 0.027519689920733725, 0.07353910524340095]
Could someone please help me do this?
Thank you in advance, I appreciate any help at all!!

채택된 답변

Kirby Fears
Kirby Fears 2015년 10월 28일
편집: Kirby Fears 2015년 10월 28일
This is not a sophisticated or flexible parser, but it works on your data in a way that is relatively easy to understand and modify.
fid = fopen('images.txt');
res = textscan(fid,'%s','Delimiter',{'{','},','}'});
fclose(fid);
data = res{1};
data = data(cellfun(@(c)~isempty(c),data));
data = reshape(data,2,numel(data)/2);
A = cellfun(@(c)c(end-20:end-6),data(1,:),'UniformOutput',false);
B = cellfun(@(c)str2num(c),data(2,:)','UniformOutput',false);
B = cell2mat(B)';
Hope this helps.
  댓글 수: 1
Jerone
Jerone 2015년 10월 28일
Thanks it helped a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by