extract part of a string from array of strings

조회 수: 4 (최근 30일)
Jahandar Jahanipour
Jahandar Jahanipour 2016년 12월 20일
댓글: Jahandar Jahanipour 2016년 12월 20일
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

채택된 답변

KSSV
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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by