Find specific strings in text files
1 次查看(过去 30 天)
显示 更早的评论
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!!
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!