How to slice each string in a string array without using for loop
Afficher commentaires plus anciens
For a string array, for example,
celldata =
3×1 cell array
{'2018-12-12'}
{'2018-11-05'}
{'2018-09-02'}
Is there array operation (i.e. without using for loop) to extract the months from each cell and convert them to a 3*1 numeric matrix, which should be [12;11;09]. I don't want to use for loop because it was too slow.
Réponse acceptée
Plus de réponses (4)
Christopher Wallace
le 19 Sep 2018
chardata = cell2mat(a);
numdata = str2num(chardata(:,6:7));
1 commentaire
Cedric
le 19 Sep 2018
No, I am CW ;-)
str2double(regexp(celldata,'(?<=-)\d+(?=-)','match','once'))
ans =
12
11
9
Star Strider
le 19 Sep 2018
celldata = [{'2018-12-12'}
{'2018-11-05'}
{'2018-09-02'}];
dt = datetime(celldata);
M = month(dt)
M =
12
11
9
Akira Agata
le 19 Sep 2018
Another possible solution:
celldata = [{'2018-12-12'}
{'2018-11-05'}
{'2018-09-02'}];
M = extractBetween(celldata,'-','-');
M = cellfun(@str2double,M);
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!