string 将 MATLAB 字符数组转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16320375/
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
Convert MATLAB char array to string
提问by rwolst
Starting with the MATLAB char array, A:
从 MATLAB 字符数组开始,A:
A(1,1) = 'A'
A(1,2) = 'P'
A(1,3) = 'R'
A(2,1) = 'M'
A(2,2) = 'A'
A(2,3) = 'Y'
How can this be converted to a cell of strings, B, such that:
如何将其转换为字符串单元格 B,使得:
B{1} = 'APR'
B{2} = 'MAY'
Edit: A is a cell and using the function cellstr gives the error
编辑: A 是一个单元格,使用函数 cellstr 会给出错误
Error using cellstr (line 23)
S must be 2-D.
采纳答案by Franck Dernoncourt
Use the following function: http://www.mathworks.com/help/matlab/ref/cellstr.html
使用以下函数:http: //www.mathworks.com/help/matlab/ref/cellstr.html
>> B = cellstr(A)
B =
'APR'
'MAY'
>> B{1}
ans =
APR
回答by rwolst
For a 3D char array T
对于 3D 字符数组 T
B = cellstr(T(1,:,:))
Gives the error
给出错误
Error using cellstr (line 23)
S must be 2-D.
Instead assign it to a 2D matrix first, then use 'cellstr' as Franck suggested above.
而是先将它分配给一个 2D 矩阵,然后使用上面 Franck 建议的 'cellstr'。
A(:,:) = T(1,:,:)
B = cellstr(A)