String Manipulation

조회 수: 163(최근 30일)
Mohamed Yaseen
Mohamed Yaseen 2011년 1월 25일
댓글: Jérôme 2022년 8월 18일
Hi All,
For example, I have filename called abcdefhijklm.xlsx and this file is displayed on the GUI. I wanted to remove the extension in the file name 'xlsx' and display only 'abcdefhijklm'. How can i manipulate this string.
Thanks in Advance Yaseen.

채택된 답변

Andreas Goser
Andreas Goser 2011년 1월 25일
I see two approaches. As you have a filename, you can have MATLAB take care of this by using the FILEPARTS command:
[pathstr, name, ext] = fileparts('abcdefhijklm.xlsx')
There is a generic approach for string search and string manipulation using commands like REGEXP, STRFIND and many others. Here, you would need to define the search logic yourself like "find all dots in the character array, identify the last dot and return all characters before that dot".
  댓글 수: 3
Walter Roberson
Walter Roberson 2011년 5월 12일
Please start a new Question for this topic.

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

추가 답변(1개)

Jérôme 2022년 8월 18일
Since R2016b, you can also use extractBefore.
filename_with_extension = "abcdefhijklm.xlsx";
filename_without_extension = extractBefore(filename_with_extension, ".")
filename_without_extension = "abcdefhijklm"
  댓글 수: 2
Jérôme 2022년 8월 18일
I totally agree with you, to be sure of the result regardless of the filename content, fileparts is the way to go.
I just wanted to mention this option if we know the filename content won't cause any issue, and because we can use directly the filename without extension, which in some cases can save us an intermediate line of code (I am not saying this is the thing to do, I just wanted to mention that this possibility exists).
filename_with_extension = "abcdefhijklm.xlsx";
% Display filename in one line
disp(extractBefore(filename_with_extension, "."))
% Display filename in two lines
[pathstr, name, ext] = fileparts(filename_with_extension);

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


Find more on Search Path in Help Center and File Exchange



Community Treasure Hunt

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

Start Hunting!

Translated by