Effacer les filtres
Effacer les filtres

Sorting a cell of strings using a certain character position

3 vues (au cours des 30 derniers jours)
Paramonte
Paramonte le 22 Fév 2019
Commenté : Paramonte le 25 Fév 2019
Dear all
I have a variable called var=
vart=[{'Ice011_L_3of3m.mat''}; {'Ice011_P_1of3m.mat'} ;{'Ice011_P_2of3m.mat'}]
We would need to sort the strings in the cell array using the 10th character of each string, so we would get the following result
var_sorted =[{'Ice011_P_1of3m.mat'}; {'Ice011_P_2of3m.mat'} ;{'Ice011_L_3of3m.mat'}]
Many thanks in advance
paramonte

Réponse acceptée

Stephen23
Stephen23 le 22 Fév 2019
Modifié(e) : Stephen23 le 22 Fév 2019
Simpler in two lines:
vart = {'Ice011_L_3of3m.mat';'Ice011_P_1of3m.mat';'Ice011_P_2of3m.mat'}
[~,idx] = sort(cellfun(@(v)v(10),vart));
vart = vart(idx)
If you need to match more than one digit you could use a regular expression, e.g.:
[~,idx,] = sort(str2double(regexp(vart,'(?<=_)\d+','match','once')));
  1 commentaire
Paramonte
Paramonte le 25 Fév 2019
Thanks a lot for your time.
This has worked perfectely

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by