Using fileparts and combining with a different file name

Hi,
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 (TRMM_1998_01_0103_newntcl.nc)
Any idea? or any help is appreciated.
cd ('C:\Users\Desktop')
input_file='TRMM_1998_01_0103_newntcl.csv';
[~, fName, ext] = fileparts(input_file);
filenc=fName.'nc';

 채택된 답변

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개)

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('%s.nc', baseFileName);
outputFile = fullfile(folder, newBaseName); % Prepend the folder.

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2015년 3월 24일

댓글:

2015년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by