Matlab入门-变量Cell array

1、cell array的声明。

方式1
>> a(1,1)={[1 2 3; 4 5 6]};%大括号括起来。
>> a(1,2)={'sasasa'};
>> a(2,1)={3+7i};
>> a(2,2)={-pi:pi:pi};
>> a

a =

  2×2 cell 数组

    [2×3 double]    'sasasa'    
    [3.0000 + 7.0000i]    [1×3 double]
////////////////////////////////////////////////////////////////////////////////
方式2
>> a{1,1}=[12 2 3;4 5 6];%前面索引便是大括号;
>> a{1,2}='sasfa';
>> a

a =

  1×2 cell 数组

    [2×3 double]    'sasfa'

>> 

2 读取cell array中内容

>> a{1,1}=[12 2 3;4 5 6];
>> a{1,2}='sasfa';
>> a

a =

  1×2 cell 数组

    [2×3 double]    'sasfa'

>> a(1,1)%小括号

ans =

  cell

    [2×3 double]

>> a{1,1}%大括号

ans =

    12     2     3
     4     5     6

>> 

 3、Mutidimensional Array:多维cell array的创建。用命令cat()

>> a{1,1}=[1 2; 3 4];
>> a{1,2}='strng';
>> a{2,1}=1+2i;
>> a{2,2}=0:1:3;
>> b{1,1}=[3 4 ;5 6];
>> b{1,2}='hjkda';
>> b{2,1}=3+9i;
>> b{2,2}=0:2;4;
>> c=cat(3,a,b)
 2×2×2 cell 数组

c(:,:,1) = 

    [2×2 double]    'strng'     
    [1.0000 + 2.0000i]    [1×4 double]


c(:,:,2) = 

    [2×2 double]    'dsakl'     
    [3.0000 + 9.0000i]    [1×3 double]

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
>> d=cat(1,a,b)%第一个参数为1时是row的合并。

d =

  4×2 cell 数组

    [2×2 double]    'strng'     
    [1.0000 + 2.0000i]    [1×4 double]
    [2×2 double]    'dsakl'     
    [3.0000 + 9.0000i]    [1×3 double]

>> e=cat(2,a,b)%第一个参数为2时是列的合并。

e =

  2×4 cell 数组

    [2×2 double]    'strng'         [2×2 double]    'dsakl'     
    [1.0000 + 2.0000i]    [1×4 double]    [3.0000 + 9.0000i]    [1×3 double]

 4、reshape()指令,

>> A=[1:3;4:6];
>> B=reshape(A,3,2)

B =

     1     5
     4     3
     2     6
//////原来的行数乘列数等于现在的行数乘列数。

5、如何存储下载工作空间中的数据

%把工作空间中产生的数据存储到本地
>> clear
>> a=magic(4);
>> save mydata.mat%用记事本打开乱码
>> save madata2.mat -ascii%用记事本打开不是乱码
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
%在matlab中打开本地中有的数据
>> load('mydata.mat')%存储的内容更多,可以记住原变量的名称
>> a

a =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


%或者
>> load('madata2.mat','-ascii')%不能记住原变量名
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
保存特定变量
>> save('hh.mat','b','-ascii')
文件名、变量名、格式

6、存储变量

>> for i=1:11
fprintf(fid,'%5.3f %8.4f',x(i),y(i));
end
>> fclose(fid);
>> type sin.txt;

0.000   0.00000.314   0.30900.628   0.58780.942   0.80901.257   0.95111.571   1.00001.885   0.95112.199   0.80902.513   0.58782.827   0.3090
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%把记事本中的内容读到matlab
>> fid=fopen('hhh.txt','r');
>> i=1;
>> while~feof(fid)
name(i,:)=fscanf(fid,'%5c',1);
no1(i)=fscanf(fid,'%d',1);
no2(i)=fscanf(fid,'%d',1);
no3(i)=fscanf(fid,'%g\n');
i=i+1;
end
>> name

name =

jon  
jikf 
sf   

>> no1

no1 =

        1998        1997        1997

>> no2

no2 =

    12    56    23

>> no3

no3 =

    5.6700   45.3000   23.4000

  Options : History : Feedback : Donate Close

猜你喜欢

转载自blog.csdn.net/yyyllla/article/details/83040608