How to modify a list of filenames in a specific way?

2 vues (au cours des 30 derniers jours)
Amanda
Amanda le 15 Déc 2020
Commenté : Amanda le 15 Déc 2020
Dear Matlab community,
I have a fairly simple question for you experts. I'd like make a column taking just the first part of a file name (string):
e.g. This is what I have
names =
"vul1_4_9.txt"
"bat1_1_1.txt"
"chy1_1_16.txt"
"chy1_1_17.txt"
"g6_1_18.txt"
"stcv1_1_1.txt"
"ser1_3_4.txt"
And I'd like to have instead a column just with the names before the first "_" :
names=
"vul1"
"bat1"
"chy1"
"chy1"
"g6"
"stcv1"
"ser1"
So far, I was able to "substract" the ".txt" part of the file name using the following loop:
names_n = length(names)
for id = 1:names_n
[~, f,ext] = fileparts(names(id));
rename = strcat(f - '_*_*') ;
names_new = [names_new;rename]
end
Obtaining this output:
names_new=
"vul1_4_9"
"bat1_1_1"
"chy1_1_16"
"chy1_1_17"
"g6_1_18"
"stcv1_1_1"
"ser1_3_4"
How should I proceed?

Réponse acceptée

Stephen23
Stephen23 le 15 Déc 2020
S = ["vul1_4_9.txt"
"bat1_1_1.txt"
"chy1_1_16.txt"
"chy1_1_17.txt"
"g6_1_18.txt"
"stcv1_1_1.txt"
"ser1_3_4.txt"];
Z = extractBefore(S,'_')
Z = 7×1 string array
"vul1" "bat1" "chy1" "chy1" "g6" "stcv1" "ser1"

Plus de réponses (0)

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by