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)
  1 件のコメント
Jahandar Jahanipour
Jahandar Jahanipour 2016 年 12 月 20 日
It couldn't be written better.
Thanks KSSV

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by