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

2 ビュー (過去 30 日間)
Amanda
Amanda 2020 年 12 月 15 日
コメント済み: Amanda 2020 年 12 月 15 日
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?
  1 件のコメント
Amanda
Amanda 2020 年 12 月 15 日
Thank you SO much!

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2020 年 12 月 15 日
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"

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by