string Matlab:将数字数组转换为字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12164752/
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
Matlab: convert array of number to array of strings
提问by olamundo
How can I convert [12 25 34 466 55]
to an array of strings ['12' '25' '34' '466' '55']
? The conversion functions I know convert that array to one string representing the entire array.
如何转换[12 25 34 466 55]
为字符串数组['12' '25' '34' '466' '55']
?我知道的转换函数将该数组转换为代表整个数组的一个字符串。
回答by Peter
An array of strings has to be a cell array. That said:
字符串数组必须是元胞数组。那说:
s = [12 25 34 466 55]
strtrim(cellstr(num2str(s'))')
回答by Kavka
Using arrayfun
together with num2str
would work:
使用arrayfun
连同num2str
将工作:
>> A = [12 25 34 466 55]
A =
12 25 34 466 55
>> arrayfun(@num2str, A, 'UniformOutput', false)
ans =
'12' '25' '34' '466' '55'
回答by Roun
Now after MATLAB 2016b, you can simply use
现在在 MATLAB 2016b 之后,您可以简单地使用
s = [12 25 34 466 55];
string(s)
回答by MiB_Coder
Starting from R2016b there is also the compose function:
从 R2016b 开始,还有 compose 函数:
>> A = [12 25 34 466 55]
A =
12 25 34 466 55
>> compose("%d", A)
ans =
1×5 string array
"12" "25" "34" "466" "55"'''
回答by Vish
In MATLAB, ['12' '25' '34' '466' '55'] is the same as a single string containing those numbers. That is to say:
在 MATLAB 中, ['12' '25' '34' '466' '55'] 与包含这些数字的单个字符串相同。也就是说:
['12' '25' '34' '466' '55']
ans =
12253446655
I need more context here for what you are trying to accomplish, but assuming you want to still be able to access each individual number as a string, a cell array is probably the best approach you can take:
我需要更多上下文来了解您要完成的任务,但假设您仍然希望能够将每个单独的数字作为字符串访问,那么元胞数组可能是您可以采用的最佳方法:
A = [1 2 3]
num2cell(num2str(A))
(Of course, you'd still have to remove the stray spaces from the ans)
(当然,您仍然必须从 ans 中删除杂散空间)