Delete last 4 characters in a string variable

In my script, I have used the 'uigetfile' function to acquire the FileName and PathName of a .txt file in order to read it. Later in the script I need to parse that file and write the parsed information to another .txt file. I want to name the file I am writing to to a similar name as the file I'm reading from. Example:
Open/read from: C:\Documents and Settings\blah\TestFile.txt
Write/parsed to: C:\Documents and Settings\blah\TestFileParsed.txt
Basically how do I delete the last four characters in a string variable? Thanks for the help!

 채택된 답변

Matt Fig
Matt Fig 2011년 3월 29일

15 개 추천

For example:
S = 'Mystringname.txt'
S = S(1:end-4)

댓글 수: 7

Ryan
Ryan 2011년 3월 29일
You, sir, are a steely-eyed MatLab genius.
How can you tell? From his picture, I'd say he looks more like a brown-eyed MATLAB genius.
Matt Fig
Matt Fig 2011년 3월 29일
Or at least a brown-eyed MATLAB fan.
Ryan
Ryan 2011년 4월 5일
It's from his expression on his face. He just has that "yep, I'm a MatLab Genius" smirk.
Yeh, but the question is how you can tell he is steely-eyed instead of brown-eyed ?
I love how you changed your picture after this whole discussion. Anyways, 7 years after this discussion ended you helped me out. Thank you.
Just for the sake of correctness, the accepted answer is applicable to character vectors.
The answer would not work for an actual string, which is initialized using double quotes as opposed to the single quotes used to initialize character arrays. The following code will produce an error:
S = "Mystringname.txt"
S = S(1:end-4)
You can check the type of a variable using
class(my_variable)

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 3월 29일

3 개 추천

Locating and removing the suffix:
idx = find(ismember(S,'./\:'),1,'last');
if S(idx) == '.'; S(idx:end) = []; end
The above includes protections against the possibility that the final component of the path does not include a '.'
If you are sure as sure as sure can be that the final component includes a '.', then
S(find(S=='.',1,'last'):end) = [];

댓글 수: 1

Hi! I'm just curious to learn more about your suggestion. First, why did you include
'./\:'
in the string to compare S to, instead of just
'.'
?
Aren't we only interested in the period? It seems to me it should be
idx = find(ismember(S,'.'),1,'last');
Secondly, what is the benefit to using it this way of wrapping the find() function around the ismember() function, instead of using strfind() function?
Thanks

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

Steven Lord
Steven Lord 2018년 11월 15일

2 개 추천

If you just want to remove the extension, break the filename into its parts using fileparts.

댓글 수: 1

fileparts is good because it does not assume that the extension is 3 characters long.

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

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

제품

질문:

2011년 3월 29일

댓글:

2019년 1월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by