Insert a string into code

3 views (last 30 days)
ly
ly on 20 Oct 2015
Commented: ly on 20 Oct 2015
I want to insert a string in to code:
...
name='time';
filename = 'name_v1.txt';
...
Can the 'time' be inserted in " name_v1.txt"?

Accepted Answer

Geoff Hayes
Geoff Hayes on 20 Oct 2015
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 Comment
ly
ly on 20 Oct 2015
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'.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings 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!