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

11 ビュー (過去 30 日間)
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()?

採用された回答

Star Strider
Star Strider 2015 年 4 月 12 日
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 件のコメント
Star Strider
Star Strider 2015 年 4 月 12 日
My pleasure!
Image Analyst
Image Analyst 2015 年 4 月 12 日
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?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by