三种数组是:数值型矩阵,字符数组(或字符串),单元数组(也有叫元胞数组)
1、数值型矩阵转换为字符数组(num2str、mat2str)
>> A=[1,2,3;4,5,6];
>> B=num2str(A)
B =
1 2 3
4 5 6
>> whos
Name Size Bytes Class Attributes
A 2x3 48 double
B 2x7 28 char
2、字符数组转换为数值型(str2num<4位精度>、str2double)
>> B&#61;[&#39;123.34&#39;];
>> A&#61;str2num(B)
A &#61;
123.3400
>> whos
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x6 12 char
3、单元数组转换为字符数组(cell2mat)&#xff1a;
例如&#xff1a;
>> A&#61;{&#39;sfjsdlgksdgjlsdjgkl&#39;};
>> B&#61;cell2mat(A)
B &#61;
sfjsdlgksdgjlsdjgkl
>> whos
Name Size Bytes Class Attributes
A 1x1 98 cell
B 1x19 38 char
4、字符数组转换为单元数组(cellstr)
>> A&#61;[&#39;what&#39;;&#39;edit&#39;;&#39;play&#39;];%注意&#xff1a;用A&#61;[]生成字符数组是&#xff0c;必须保证每行有相同的长度&#xff0c;不够可用空格补
>> B&#61;cellstr(A)
B &#61;
&#39;what&#39;
&#39;edit&#39;
&#39;play&#39;
>> whos
Name Size Bytes Class Attributes
A 3x4 24 char
B 3x1 204 cell
5、单元数组转换为数值型矩阵
>> A&#61;{&#39;123.4&#39;};
>> B&#61;cell2mat(A)
B &#61;
123.4
>> C&#61;str2num(B)
C &#61;
123.4000
>> whos
Name Size Bytes Class Attributes
A 1x1 70 cell
B 1x5 10 char
C 1x1 8 double
6、数值型矩阵转换为单元数组(num2cell)
>> A&#61;[1,2,3;4,5,6];
>> B&#61;num2cell(A)
B &#61;
[1] [2] [3]
[4] [5] [6]
>> whos
Name Size Bytes Class Attributes
A 2x3 48 double
B 2x3 408 cell