Using fileparts and combining with a different file name

50 ビュー (過去 30 日間)
Damith 2015 年 3 月 24 日
コメント済み: Damith 2015 年 3 月 25 日
I need to strip the file name and extension (TRMM_1998_01_0103_newntcl.csv) to TRMM_1998_01_0103_newntcl and .csv and then combine the file name with different extension (.nc) and then write the complete file name. I think I am close to that but the last line does not give me the desired output (
Any idea? or any help is appreciated.
cd ('C:\Users\Desktop')
[~, fName, ext] = fileparts(input_file);


David Young
David Young 2015 年 3 月 24 日
The last line should be
filenc = [fName '.nc'];
What you are doing here is concatenating two strings. Square brackets can be used to concatenate any kind of array, including strings. (Your previous syntax, with a dot, is used for structures.)
  1 件のコメント
Damith 2015 年 3 月 25 日


その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 3 月 25 日
Another option, if you know for certain what the first extension is, is to use strrep():
input_file = strrep(input_file, '.csv', 'nc');
No need to call file parts. Or, if you do use fileparts
[folder, baseFileName, ext] = fileparts(input_file);
newBaseName = sprintf('', baseFileName);
outputFile = fullfile(folder, newBaseName); % Prepend the folder.
  1 件のコメント
Damith 2015 年 3 月 25 日


Community Treasure Hunt

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

Start Hunting!

Translated by