extract part of a string from array of strings

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

KSSV
KSSV il 20 Dic 2016
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)

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by