How do you add a file extension when creating a file based on function input?

47 views (last 30 days)
I want to be able to use an input for a function and create a text file based on the fid. The strjoin() function always inputs a space in between the fid and the '.txt'. This is what I have now, but I need a more optimal method.
function[...] = name(fid)
textfile = strjoin({fid,'.txt'})
end
The output would then look like 'fid .txt'. I need to have the space removed. Is there any better function than using strjoin()?

Accepted Answer

Star Strider
Star Strider on 12 Apr 2015
I’m not certain what you want (because I don’t know what ‘fid’ is), but see if:
textfile = [fid,'.txt'];
does what you want.
Example:
fid = 'FileNamePrefix';
textfile = [fid,'.txt']
produces:
textfile =
FileNamePrefix.txt
  3 Comments
Image Analyst
Image Analyst on 12 Apr 2015
An alternate way, useful for when the filenames are really complicated
textfile = sprintf('%s.txt', fid);
Note that fid is the variable name commonly given to the output of fopen() and not to the base file name string, so I recommend you choose a different name for the fid variable, like, how about baseFileName?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!