Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Save file with different extension

Asked by AsYouWish

AsYouWish (view profile)

on 11 Dec 2012


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:

[FileName,PathName] = uigetfile('*.*','Select File to Plot',DefaultFile); %choose file to open.
%%%% **************** Read the File ************* 

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');




AsYouWish (view profile)


1 Answer

Answer by Image Analyst

Image Analyst (view profile)

on 11 Dec 2012
Accepted answer

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.


Image Analyst

Image Analyst (view profile)

on 11 Dec 2012

I'd do

[folder, oldBaseName, oldExt] = fileparts(oldFileName);
newBaseName = sprintf('%s.txt',oldBaseName);
newFullFuleName = fullfile(folder, newBaseName);

AsYouWish (view profile)

on 11 Dec 2012

Thanks. I used that and it makes it easier to identify original and new file names when looking at the code.

Jan Simon

Jan Simon (view profile)

on 12 Dec 2012

@AsYouWish: Please accept this answer, if it solves your problem. Thanks.

Image Analyst

Image Analyst (view profile)

Contact us