Eliminate string from vector
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Giacomo Abrardo
le 18 Juin 2021
Commenté : Giacomo Abrardo
le 19 Juin 2021
Hi, i created a vector containing 963 nc file. The problem is that some of them are the same but from two different version and i want to delete the previous version. For example, how can i eliminate row 844, 846 and 848 from my vector? Thanks all![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/658145/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/658145/image.jpeg)
0 commentaires
Réponse acceptée
Image Analyst
le 19 Juin 2021
You say "For example from c_gls to S1CSAR" so basically up until the last underline. Don't include _V and anything after that. This will do it:
% Create sample data
ca = {...
'c_gls_SSM1km_202007310000_CEURO_S1CSAR_V1.1.1.NC';...
'c_gls_SSM1km_202007310000_CEURO_S1CSAR_V1.1.2.NC';...
'c_gls_SSM1km_202008040000_CEURO_S1CSAR_V1.1.1.NC';...
'c_gls_SSM1km_202008040000_CEURO_S1CSAR_V1.1.2.NC';...
'c_gls_SSM1km_202008050000_CEURO_S1CSAR_V1.1.1.NC';...
'c_gls_SSM1km_202008050000_CEURO_S1CSAR_V1.1.2.NC'}
% Loop over each cell replacing it with the contents
% but only until the last underline.
for k = 1 : length(ca)
underlineIndexes = find(ca{k} == '_');
% Take up until the last underline
ca{k} = ca{k}(1:underlineIndexes(end) - 1);
end
ca = unique(ca) % Get unique and show results in command window.
You get
ca =
3×1 cell array
{'c_gls_SSM1km_202007310000_CEURO_S1CSAR'}
{'c_gls_SSM1km_202008040000_CEURO_S1CSAR'}
{'c_gls_SSM1km_202008050000_CEURO_S1CSAR'}
Is that what you want?
If you know that the last underline is always in the same location, you could simplify it to be
for k = 1 : length(ca)
ca{k} = ca{k}(1:38); % Extract the first 38 characters of the kth cell.
end
OK, 3 lines instead of 1 for the regexp() way, but you might find this more intuitive and less cryptic.
Plus de réponses (1)
Image Analyst
le 18 Juin 2021
Did you try the unique() function? It has lots of options so be sure you understand which options to use.
If you need more help, attach your cell array in a .mat file with the paperclip icon.
Voir également
Catégories
En savoir plus sur Characters and Strings 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!