Matlab三种数组间的转换

三种数组是:数值型矩阵,字符数组(或字符串),单元数组(也有叫元胞数组)

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=['123.34'];
>> A=str2num(B)

A =

  123.3400

>> whos
  Name      Size            Bytes  Class     Attributes

  A         1x1                 8  double             
  B         1x6                12  char   

 

3、单元数组转换为字符数组(cell2mat):

例如:

>> A={'sfjsdlgksdgjlsdjgkl'};
>> B=cell2mat(A)

B =

sfjsdlgksdgjlsdjgkl

>> whos
  Name      Size            Bytes  Class    Attributes

  A         1x1                98  cell              
  B         1x19               38  char         

 

4、字符数组转换为单元数组(cellstr)

>> A=['what';'edit';'play'];%注意:用A=[]生成字符数组是,必须保证每行有相同的长度,不够可用空格补
>> B=cellstr(A)

B =

    'what'
    'edit'
    'play'

>> whos
  Name      Size            Bytes  Class    Attributes

  A         3x4                24  char              
  B         3x1               204  cell    

 

5、单元数组转换为数值型矩阵

>> A={'123.4'};
>> B=cell2mat(A)

B =

123.4

>> C=str2num(B)

C =

  123.4000

>> whos
  Name      Size            Bytes  Class     Attributes

  A         1x1                70  cell               
  B         1x5                10  char               
  C         1x1                 8  double 

 

6、数值型矩阵转换为单元数组(num2cell)

>> A=[1,2,3;4,5,6];
>> B=num2cell(A)

B =

    [1]    [2]    [3]
    [4]    [5]    [6]

>> whos
  Name      Size            Bytes  Class     Attributes

  A         2x3                48  double             
  B         2x3               408  cell  


原文地址:http://blog.sina.com.cn/s/blog_5e73a8fc0100rrj6.html

你可能感兴趣的:(matlab)