Save file with different extension

Hi,
I am a newbie and am opening some files, adding some lines, then saving the files. I can't figure out how to save each file I open with a new extension while keeping the filename the same.
Here is how I am opening the files:
DefaultFile='';
[FileName,PathName] = uigetfile('*.*','Select File to Plot',DefaultFile); %choose file to open.
%%%%**************** Read the File *************
fid=fopen(fullfile(PathName,FileName));
To save the file do I use the following fopen and if so what do I put where the question marks are?
fileID = fopen(??????,'w');
Thanks

 채택된 답변

Image Analyst
Image Analyst 2012년 12월 11일

2 개 추천

You have another fopen() to open an output file. Your question marks should be replaced by the full file name of your output file. You can use sprintf() (if needed) and fullfile() to create that output file name. Then use fprintf to write text to it. Then call fclose() to close the two files.

댓글 수: 7

Not sure I completely understand but here is what I did.
1. I replaced the question marks with FileName
fileID = fopen(FileName,'w');
2. The file was saved but with the original name AND extension. How do I replace the original extension with a new one (say data.dat to data.txt)
Thanks.
I just tried this:
[pathstr, name, ext] = fileparts(FileName)
Which worked to separate the file name from the extension. Now, how do I print the file by concatenating the name with the extension?
This saves the file without the extension. How do I add a ".txt" extension?
fileID = fopen(name,'w');
Thanks.
UJJWAL
UJJWAL 2012년 12월 11일
you can use horzcat(name,newext) , where newext is the new extension u wanna give.. and then fileID = fopen(horzcat(name,newext),'w');
Thanks. Just before you answered I used:
fname = strcat(name, '.txt');
fileID = fopen(fname,'w');
Thanks everyone for their help.
I'd do
[folder, oldBaseName, oldExt] = fileparts(oldFileName);
newBaseName = sprintf('%s.txt',oldBaseName);
newFullFuleName = fullfile(folder, newBaseName);
AsYouWish
AsYouWish 2012년 12월 11일
Thanks. I used that and it makes it easier to identify original and new file names when looking at the code.
Jan
Jan 2012년 12월 12일
@AsYouWish: Please accept this answer, if it solves your problem. Thanks.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by