string 如何连接字符串+ i?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8420147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to concat string + i?
提问by simpatico
for i=1:N
f(i) = 'f'+i;
end
gives an error in MatLab. What's the correct syntax to initialize an array with N strings of the pattern fi?
在 MatLab 中给出错误。用模式 fi 的 N 个字符串初始化数组的正确语法是什么?
It seems like even this is not working:
似乎即使这样也行不通:
for i=1:4
f(i) = 'f';
end
回答by Mansoor Siddiqui
You can concatenate strings using strcat
. If you plan on concatenating numbers as strings, you must first use num2str
to convert the numbers to strings.
您可以使用 连接字符串strcat
。如果您计划将数字连接为字符串,则必须首先使用num2str
将数字转换为字符串。
Also, strings can't be stored in a vector or matrix, so f
must be defined as a cell array, and must be indexed using {
and }
(instead of normal round brackets).
此外,字符串不能存储在向量或矩阵中,因此f
必须定义为元胞数组,并且必须使用{
and }
(而不是普通的圆括号)进行索引。
f = cell(N, 1);
for i=1:N
f{i} = strcat('f', num2str(i));
end
回答by gnovice
For versions prior to R2014a...
对于 R2014a 之前的版本...
One easy non-loop approach would be to use genvarname
to create a cell arrayof strings:
一种简单的非循环方法是使用genvarname
创建字符串元胞数组:
>> N = 5;
>> f = genvarname(repmat({'f'}, 1, N), 'f')
f =
'f1' 'f2' 'f3' 'f4' 'f5'
For newer versions...
对于较新的版本...
The function genvarname
has been deprecated, so matlab.lang.makeUniqueStrings
can be used instead in the following way to get the same output:
该函数genvarname
已被弃用,因此matlab.lang.makeUniqueStrings
可以通过以下方式使用以获取相同的输出:
>> N = 5;
>> f = strrep(matlab.lang.makeUniqueStrings(repmat({'f'}, 1, N), 'f'), '_', '')
f =
1×5 cell array
'f1' 'f2' 'f3' 'f4' 'f5'
回答by Amro
Let me add another solution:
让我添加另一个解决方案:
>> N = 5;
>> f = cellstr(num2str((1:N)', 'f%d'))
f =
'f1'
'f2'
'f3'
'f4'
'f5'
If N
is more than two digits long (>= 10
), you will start getting extra spaces. Add a call to strtrim(f)
to get rid of them.
如果N
长度超过两位数 ( >= 10
),您将开始获得额外的空格。添加呼叫以strtrim(f)
摆脱它们。
As a bonus, there is an undocumented built-in function sprintfc
which nicely returns a cell arrays of strings:
作为奖励,有一个未记录的内置函数sprintfc
可以很好地返回字符串元胞数组:
>> N = 10;
>> f = sprintfc('f%d', 1:N)
f =
'f1' 'f2' 'f3' 'f4' 'f5' 'f6' 'f7' 'f8' 'f9' 'f10'
回答by luator
Using sprintf
was already proposed by ldueck in a comment, but I think this is worth being an answer:
使用sprintf
已经由 ldueck 在评论中提出,但我认为这值得作为一个答案:
f(i) = sprintf('f%d', i);
This is in my opinion the most readable solution and also gives some nice flexibility (i.e. when you want to round a float value, use something like %.2f
).
在我看来,这是最易读的解决方案,并且还提供了一些很好的灵活性(即,当您想要舍入浮点值时,请使用类似%.2f
)。
回答by Andres
回答by Hiva
Try the following:
请尝试以下操作:
for i = 1:4
result = strcat('f',int2str(i));
end
If you use this for naming several files that your code generates, you are able to concatenate more parts to the name. For example, with the extension at the end and address at the beginning:
如果您使用它来命名代码生成的多个文件,则可以将更多部分连接到名称。例如,以扩展名结尾和地址开头:
filename = strcat('c:\...\name',int2str(i),'.png');