split the name of a file (image .png)

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2022년 11월 25일
댓글: Image Analyst 2022년 11월 25일
I would like to change the name of this image:
'name_of_figure_BN_102.png'
to this name:
'name_of_figure_NB_102.png'
I tried splitting the name with 'strsplit' but it doesn't apply to my picture name because there is this '_' symbol.
Is there a simple method to change the name?
  댓글 수: 3
Alberto Acri
Alberto Acri 2022년 11월 25일
just one-time thing
Image Analyst
Image Analyst 2022년 11월 25일
OK, fine, but did you see the Answers below?

댓글을 달려면 로그인하십시오.

채택된 답변

Vilém Frynta
Vilém Frynta 2022년 11월 25일
My way to do so would be as follows:
str1 = "name_of_figure_BN_102.png"; % Your input image name
strPieces = strsplit(str1,"_") % Splitting the name into pieces
strPieces = 1×5 string array
"name" "of" "figure" "BN" "102.png"
strPieces(4) = "NB" % Editing "BN" to "NB"
strPieces = 1×5 string array
"name" "of" "figure" "NB" "102.png"
% Reconstructing the name again
str2 = strcat(strPieces(1),"_", strPieces(2),"_", strPieces(3),"_", strPieces(4),"_", strPieces(5))
str2 = "name_of_figure_NB_102.png"
I am certain that there is a better way of reconstructing str2 and also there is a way to detect BN's position (and automatize this process). I only had few minutes to make this up. Hope it helps.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 11월 25일
Here is one way:
fileName = 'name_of_figure_BN_102.png';
underlineLocations = strfind(fileName, '_')
underlineLocations = 1×4
5 8 15 18
index1 = underlineLocations(end-1) + 1;
index2 = underlineLocations(end) - 1;
fileName(index1 : index2) = fliplr(fileName(index1 : index2))
fileName = 'name_of_figure_NB_102.png'

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by