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!
採用された回答
その他の回答 (2 件)
Walter Roberson
2011 年 3 月 29 日
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 件のコメント
Alexei
2014 年 7 月 30 日
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
2018 年 11 月 15 日
2 投票
1 件のコメント
Walter Roberson
2018 年 11 月 16 日
fileparts is good because it does not assume that the extension is 3 characters long.
カテゴリ
ヘルプ センター および File Exchange で Scope Variables and Generate Names についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!