extract part of a string from array of strings
2 views (last 30 days)
Show older comments
Jahandar Jahanipour
on 20 Dec 2016
Commented: Jahandar Jahanipour
on 20 Dec 2016
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 Comments
Accepted Answer
KSSV
on 20 Dec 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)
More Answers (0)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!