Insert a string into code

3 次查看(过去 30 天)
ly
ly 2015-10-20
评论: ly 2015-10-20
I want to insert a string in to code:
...
name='time';
filename = 'name_v1.txt';
...
Can the 'time' be inserted in " name_v1.txt"?

采纳的回答

Geoff Hayes
Geoff Hayes 2015-10-20
ly - if the idea is to replace the 'name' substring within 'name_v1.txt' with 'time', then you could use strrep to accomplish this. For example,
filename = 'name_v1.txt';
filename = strrep(filename,'name','time');
would set filename to
filename =
time_v1.txt
Or, you could use sprintf to do something similar
name = 'time';
filename = sprintf('%s_v1.txt',name);
which would (agains) set filename to 'time_v1.txt'.
  1 个评论
ly
ly 2015-10-20
This is what I need:
Or, you could use sprintf to do something similar
name = 'time';
filename = sprintf('%s_v1.txt',name);
which would (agains) set filename to 'time_v1.txt'.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by