Renaming a file the same as a variable

1 回表示 (過去 30 日間)
Tyler Murray
Tyler Murray 2016 年 10 月 3 日
コメント済み: Tyler Murray 2016 年 10 月 3 日
I am trying to rename a file the same as a variable. The variable obviously will be string, its just so I can make my program even more automated. For example :
x = 'RenameTest'
movefile('Original.txt', '%s.txt', x)
That code doesn't work but is this possible to do?
  2 件のコメント
Peter Gruber
Peter Gruber 2016 年 10 月 3 日
You are amost there. The right way to do it is
x = 'RenameTest'
movefile('Original.txt', [x '.txt'])
or, if you really want to use the formatting string (not really needed)
x = 'RenameTest'
movefile('Original.txt', sprintf('%s.txt', x))
Tyler Murray
Tyler Murray 2016 年 10 月 3 日
Thanks Peter! Works great!

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

採用された回答

dpb
dpb 2016 年 10 月 3 日
As written, just
movefile('Original.txt', [x '.txt'])
More general and where you were probably trying to head would be something like
outfile=sprintf('%s.txt',newfilebase);
movefile(oldfile,outfile)
where oldfile is a string variable containing the original name.
  1 件のコメント
Tyler Murray
Tyler Murray 2016 年 10 月 3 日
Thanks dpb!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by