好文档 - 专业文书写作范文服务资料分享网站

matlab经典源程序带有注释(详细经典)

天下 分享 时间: 加入收藏 我要投稿 点赞

%判断A是否为元胞数组,如果为元胞数组,则函数 >> tf = iscell(A) tf = 1

%示例5:显示元胞数组C中的内容 >> clear

>> C={'Smith' [1 2;3 4] [12]}; %直接显示元胞数组C中的内容 >> celldisp(C) C{1} = Smith C{2} = 1 2 3 4 C{3} = 12

%显示元胞数组C中的内容,数组的名称用cellcontent代替 >> celldisp(C,'cellcontent') cellcontent{1} = Smith

cellcontent{2} = 1 2 3 4 cellcontent{3} = 12

%示例6:将字符数组转换为元胞数组 >> S = ['abc '; 'defg'; 'hi m']; >> cellstr(S) ans =

'abc' %原先abc后面的空格被清除 'defg'

'hi m' %i和m之间的空格仍然保留

%示例7:显示元胞数组S中的内容(包括空格和字符) >> S = {'abc ', 'defg','hi m'}; >> cellplot(S)

%示例8:将数字数组A按行或按列转换为元胞数组 %A是4x3的数组

>> A=[1 2 3;4 5 6;7 8 9;10 11 12];

%把A的每一列转换为一个元胞,得到的C是1×3的元胞数组 >> C=num2cell(A,1) C =

[4x1 double] [4x1 double] [4x1 double] %把A的每一行转换为一个元胞,得到的C是4×1的元胞数组 >> C=num2cell(A,2) C =

[1x3 double] [1x3 double] [1x3 double]

[1x3 double]

2.4结构数组

%示例1:使用直接法来创建结构数组 >> A(1).name = 'Pat'; A(1).number = 176554; A(2).name = 'Tony'; A(2).number = 901325; >> A A =

1x2 struct array with fields: name number

%示例2:利用struct函数来创建结构数组 >> A(1)=struct('name','Pat','number',176554); A(2)=struct('name','Tony','number',901325); >> A A =

1x2 struct array with fields:

name number

%示例3:使用deal函数来得到结构体中各结构域的值 %定义结构数组A

>> A.name = 'Pat'; A.number = 176554;A(2).name = 'Tony'; A(2).number = 901325;

%得到结构数组中所有name结构域的数据 >>[name1,name2] = deal(A(:).name) name1 = Pat name2 = Tony

%示例4:使用getfield函数来取得结构体中结构域的值 %定义mystr结构数组

>> mystr(1,1).name = 'alice';mystr(1,1).ID = 0;mystr(2,1).name = 'gertrude'; mystr(2,1).ID = 1;

%取得mystr(2,1)的结构域name的值 >> f = getfield(mystr, {2,1}, 'name') f = gertrude

%示例5:删除结构数组中的指定结构域 %定义结构数组s

>> s.field1=[1 2 3];s.field2='string';s.field3={1 2 3;4 5 6};

%删除结构域field1 >> s=rmfield(s,'field1') s =

field2: 'string' field3: {2x3 cell} %删除结构域'field2','field3' >> s=rmfield(s,{'field2','field3'}) s =

field1: [1 2 3] %示例6: %定义结构数组s

>> s.field1=[1 2 3];s.field2='string';s.field3={1 2 3;4 5 6}; >> s s =

field1: [1 2 3] field2: 'string' field3: {2x3 cell} %将结构数组转换为元胞数组 >> c=struct2cell(s) c =

[1x3 double] 'string' {2x3 cell } %示例7:

matlab经典源程序带有注释(详细经典)

%判断A是否为元胞数组,如果为元胞数组,则函数>>tf=iscell(A)tf=1%示例5:显示元胞数组C中的内容>>clear>>C={'Smith'[12;34][12]};%直接显示元胞数组C中的内容>>celldisp(C)C{1}=SmithC{2}=12
推荐度:
点击下载文档文档为doc格式
87gey5dute6ksx798r8q
领取福利

微信扫码领取福利

微信扫码分享