How do you add a file extension when creating a file based on function input?
47 views (last 30 days)
Show older comments
Domenick Avanzi
on 12 Apr 2015
Commented: Image Analyst
on 12 Apr 2015
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()?
0 Comments
Accepted Answer
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
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?
More Answers (0)
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!