如何连接字符串 +我?

发布于 2024-12-20 05:08:18 字数 181 浏览 5 评论 0原文

for i=1:N
   f(i) = 'f'+i;
end

在 MatLab 中给出错误。使用模式 fi 的 N 个字符串初始化数组的正确语法是什么?

似乎这也不起作用:

for i=1:4
  f(i) = 'f';
end
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?

It seems like even this is not working:

for i=1:4
  f(i) = 'f';
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我很坚强 2024-12-27 05:08:18

您可以使用 strcat 连接字符串。如果您计划将数字连接为字符串,则必须首先使用 num2str将数字转换为字符串。

此外,字符串不能存储在向量或矩阵中,因此 f 必须定义为 元胞数组,并且必须使用 {} (而不是普通的圆括号)进行索引。

f = cell(N, 1);
for i=1:N
   f{i} = strcat('f', num2str(i));
end

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.

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 = cell(N, 1);
for i=1:N
   f{i} = strcat('f', num2str(i));
end
最佳男配角 2024-12-27 05:08:18

对于 R2014a 之前的版本...

一种简单的非循环方法是使用 genvarname 创建一个 单元格字符串数组

>> N = 5;
>> f = genvarname(repmat({'f'}, 1, N), 'f')

f = 

    'f1'    'f2'    'f3'    'f4'    'f5'

对于较新的版本...

函数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'

For versions prior to R2014a...

One easy non-loop approach would be to use genvarname to create a cell array of strings:

>> 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:

>> N = 5;
>> f = strrep(matlab.lang.makeUniqueStrings(repmat({'f'}, 1, N), 'f'), '_', '')

 f =
   1×5 cell array

     'f1'    'f2'    'f3'    'f4'    'f5'
凡间太子 2024-12-27 05:08:18

让我添加另一个解决方案:

>> N = 5;
>> f = cellstr(num2str((1:N)', 'f%d'))
f = 
    'f1'
    'f2'
    'f3'
    'f4'
    'f5'

如果 N 长度超过两位数 (>= 10),您将开始获得额外的空格。添加对 strtrim(f) 的调用来消除它们。


作为奖励,有一个未记录的内置函数 sprintfc 可以很好地返回字符串元胞数组:

>> N = 10;
>> f = sprintfc('f%d', 1:N)
f = 
    'f1'    'f2'    'f3'    'f4'    'f5'    'f6'    'f7'    'f8'    'f9'    'f10'

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.


As a bonus, there is an undocumented built-in function sprintfc which nicely returns a cell arrays of strings:

>> N = 10;
>> f = sprintfc('f%d', 1:N)
f = 
    'f1'    'f2'    'f3'    'f4'    'f5'    'f6'    'f7'    'f8'    'f9'    'f10'
芸娘子的小脾气 2024-12-27 05:08:18

ldueck 在评论中已经提出使用 sprintf ,但我认为这值得作为一个答案:

f(i) = sprintf('f%d', i);

在我看来,这是最具可读性的解决方案,并且还提供了一些很好的灵活性(即,当您想要舍入时)浮点值,使用类似 %.2f 的值)。

Using sprintf was already proposed by ldueck in a comment, but I think this is worth being an answer:

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).

—━☆沉默づ 2024-12-27 05:08:18

根据 this 看起来你必须在尝试之前设置“N”要使用它,看起来它需要是一个 int 而不是字符串?对 MatLab 不太了解,但只是我从该网站收集的信息..希望它有帮助:)

according to this it looks like you have to set "N" before trying to use it and it looks like it needs to be an int not string? Don't know much bout MatLab but just what i gathered from that site..hope it helps :)

鲜肉鲜肉永远不皱 2024-12-27 05:08:18

尝试以下操作:

for i = 1:4
    result = strcat('f',int2str(i));
end

如果您使用它来命名代码生成的多个文件,您可以将更多部分连接到该名称。例如,扩展名位于末尾,地址位于开头:

filename = strcat('c:\...\name',int2str(i),'.png'); 

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'); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文