string 将字符串附加到 Matlab 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2288899/
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
Appending string to Matlab array
提问by Name
How do I append a string to a Matlab array column wise?
如何明智地将字符串附加到 Matlab 数组列?
Here is a small code snippet of what I am trying to do:
这是我正在尝试做的一个小代码片段:
for_loop
filename = 'string';
name=[name; filename]
end
回答by Amro
You need to use cell arrays. If the number of iterations are known beforehand, I suggest you preallocate:
您需要使用元胞数组。如果事先知道迭代次数,我建议你预先分配:
N = 10;
names = cell(1,N);
for i=1:N
names{i} = 'string';
end
otherwise you can do something like:
否则,您可以执行以下操作:
names = {};
for i=1:10
names{end+1} = 'string';
end
回答by gnovice
As other answers have noted, using cell arraysis probably the most straightforward approach, which will result in your variable name
being a cell array where each cell element contains a string.
正如其他答案所指出的,使用元胞数组可能是最直接的方法,这将导致您的变量name
成为一个元胞数组,其中每个元胞元素都包含一个字符串。
However, there is another option using the function STRVCAT, which will vertically concatenate strings. Instead of creating a cell array, this will create a 2-D character matrix with each row containing one string. STRVCATautomatically pads the ends of the strings with spaces if necessary to correctly fill the rows of the matrix:
但是,还有另一个选项使用函数STRVCAT,它将垂直连接字符串。这将创建一个二维字符矩阵,每行包含一个字符串,而不是创建一个元胞数组。如果需要正确填充矩阵的行,STRVCAT 会自动用空格填充字符串的末端:
>> string1 = 'hi';
>> string2 = 'there';
>> S = strvcat(string1,string2)
S =
hi
there
回答by lindelof
As noted elsewhere, in MATLAB all strings in an array must be the same length. To have strings of different lengths, use a cell array:
正如其他地方所指出的,在 MATLAB 中,数组中的所有字符串都必须具有相同的长度。要拥有不同长度的字符串,请使用元胞数组:
name = {};
for i = somearray
name = [name; {string}];
end
回答by bornTalented
Use strcat
function to append using one line code without using loop:
使用strcat
函数在不使用循环的情况下使用一行代码追加:
A={'food','banana','orange'}
A = 'food' 'banana' 'orange'
A = '食物' '香蕉' '橙子'
A = strcat(A,'s')
A = 'foods' 'bananas' 'oranges'
A = '食物' '香蕉' '橙子'
回答by Babak
name=[];
for_loop
filename = 'string';
name=[name; {filename}];
end
回答by MJay
If you are using two arrays like below (A and B) you can append them like what you do with other matrices.
如果您使用如下两个数组(A 和 B),您可以像处理其他矩阵一样附加它们。
A = {'a' ; 'b' ; 'c'};
B = {'1' ; '2' ; '3'};
Result = { A{:,1} ; B{:,1} }
Result =
'a' 'b' 'c'
'1' '2' '3'
回答by Amro
For completeness, one should also mention the new string
classintroduced in MATLAB R2016b; a container for text data along with a set of functions for easy text manipulation.
为了完整起见,还应该提到MATLAB R2016b 中引入的新string
类;文本数据的容器以及一组便于文本操作的函数。
To compare it against my other example, here is how to allocate a string array:
N = 10;
names = strings(1,N);
for i=1:N
names(i) = 'string';
end
And here is how to dynamically expand the array without preallocation:
下面是如何在没有预分配的情况下动态扩展数组:
names = strings(0);
for i=1:10
names(end+1) = 'string';
end
(Of course if the strings are all the same or form a sequence with a pattern, there are better ways to create the array without a loop. The above was just an example of filling the array one-by-one).
(当然,如果字符串全部相同或者形成具有模式的序列,则有更好的方法来创建没有循环的数组。上面只是一个一个填充数组的例子)。
The string
container can also convert to/from character arraysand cell arrays of chars.
回答by Jon Tran
I know that this is an old thread, but it seems as if you don't even need a loop if this is all you are doing in the loop. Could you just use a vertical concatenation?
我知道这是一个旧线程,但如果这就是您在循环中所做的一切,似乎您甚至不需要循环。你可以只使用垂直串联吗?
mmm = {'str1'; 'str2'; 'str3'};
temp = {'a'; 'b'; 'c'};
mmm = [mmm; temp]
mmm =
'str1'
'str2'
'str3'
'a'
'b'
'c'
回答by High Performance Mark
You are going the right way. Use {} to build a cell array, like this
你走对了路。使用 {} 构建元胞数组,如下所示
stringtable = 'a string';
for i = 1:3
stringtable = {stringtable;new_string(i)}
end
should do what you want.
应该做你想做的。