extract part of a string from array of strings
3 次查看(过去 30 天)
显示 更早的评论
I have a (10000*1) cell array (info) of file names stored like this:
info{1} = '/data/input/1001_1094.png';
info{2} = '/data/input/1001_1094.png';
info{3} = '/data/input/1209_7856.png';
...
info{10000} = '/data/input/982_123.png';
the numbers between "_" are coordinated which I want to extract. I want to store them in a matrix like below:
x y
1001 1094
1209 7856
...
982 123
I know that it can be done in a simple for loop using the following code:
for i = 1:length(info)
fName = strsplit(info{i}, '/');
cordName = strsplit(fName{end}, '.');
coorinates(i,:) = cellfun(@str2num,(strsplit(cordName {1}, '_')));
end
But since I have a very very long array of these coordinates and I prefer not to do it with for loop. I was wondering if there was any vectorized method for implementing this.
Thanks
0 个评论
采纳的回答
KSSV
2016-12-20
info{1} = '/data/input/1001_1094.png';
info{2} = '/data/input/1001_1094.png';
info{3} = '/data/input/1209_7856.png';
[~,coord,~] = cellfun(@fileparts,info,'un',0) ;
C = cellfun(@(x) strsplit(x,'_'),coord,'Un',0) ;
C = vertcat(C{:}) ;
coorinates = cellfun(@str2num,C)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!