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?

채택된 답변

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 CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by