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

9 vues (au cours des 30 derniers jours)
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()?

Réponse acceptée

Star Strider
Star Strider le 12 Avr 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 commentaires
Image Analyst
Image Analyst le 12 Avr 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?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by