construc a string from other strings and variables

1 次查看(过去 30 天)
I am just trying to construct a single string to use as a file name, from several other strings and variables. Should be simple, but I cannot get the syntax right. For example:
subj = 'A2';
ndip = '2503';
nroi = '68';
band = '2-4';
fil_nam = [subj '_b' band '_d' ndip '_s' nroi];
fil_nam is created as a cell array, so how do I force it to be a string? Or convert to string?
Thanks

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-2-10
If fil_nam is a cell array
fil_nam = {subj '_b' band '_d' ndip '_s' nroi}
out=strjoin(fil_nam)
  2 个评论
Walter Roberson
Walter Roberson 2014-2-10
strjoin() puts a space between the parts by default. You can specify '' as the delimiter to avoid the space. If you are going to do that then you might as well use
out = cat(2, fil_name{:});

请先登录,再进行评论。


per isakson
per isakson 2014-2-10
编辑:per isakson 2014-2-11
I find sprintf to be the best function to combine fixed strings and values of string (and numeric) variables into one string. Thus,
sprintf( '%s_b%s_d%s_s%s', subj, band, ndip, nroi )
returns
ans =
A2_b2-4_d2503_s68

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by