Matlab使用单元数组(cell array)和结构数组 (struct array)

标签:
structcell |
分类: Matlab |
一. 单元数组
1.1单元数组创建与显示:
1、直接赋值法:按单元索引法 和按内容索引法。(其实也就是将花括号放在等式的右边或是左边的区别)。注意:“按单元索引法”和“按内容索引法”是完全等效的,可以互换使用。通过下面实例,我们看到:花括号{}用于访问单元的值,而括号()用于标识单元(即:不用于访问单元的值)。具体理解{}和()区别可以在下面代码最后分别输入A{2,2}和A(2,2)。就会发现“按内容索引法{}”能显示完整的单元内容,而“按单元索引法()”有时无法显示完整的单元内容。
>> A(1,1)={[1 2
3; 4 5 6;7 8
9]};
>> A(1,2)={2+3i};
>> A(2,1)={'A character'};
>> A(2,2)={12:-2:0};
>>
A
A =
>> B{1,1}=[1 2 3;4 5 6;7 8
9];
>> B{1,2}=2+3i;
>> B{2,1}='A character';
>> B{2,2}=12:-2:0;
>>B
B =
2、利用cell函数法:即首先用cell函数生成一个空的单元数组,然后再向其中添加所需的数据。下面的代码生成一个2X3的空单元数组:
>> C=cell(2,3)
C =
利用cell生成空单元数组后,可以采用“按单元索引法”和“按内容索引法”对其进行赋值。在赋值时,用户一定要注意{}和()的用法。
>> C(1,1)={'This does work'}
C =
>> C{2,3}='This work'
C =
(1)利用cell指令创建单元数组
C=cell(2);
空单元数组
C(:,1)={char('Another','text string');10:-1:1}
%对第一列单元赋值
(2)单元数组的“列”扩充和“行”扩充
AC=[A
C]
A_C=[A;C]
A_C =
【例】cellplot能用图形形象化地表示单元数组的内容。(A_C取自上例)
单元数组的收缩
A_C(3,:)=[]
的单元数组
把A_C重组成
单元数组R_A_C
[1.0000+
2.0000i]
1.2 单元数组内容的调取
取一个单元
f1=R_A_C(1,3)
class(f1)
ans =
cell
取一个单元的内容
f2=R_A_C{1,3}
class(f2)
sin(-3*t)*exp(-t)
ans =
sym
取单元内的子数组
f3=R_A_C{1,1}(:,[1 2 5
6])
这是
单元创建
同时调取多个单元内容
[f4,f5,f6]=deal(R_A_C{[1,3,4]})
这是
单元数组创建算例 1
f5 =
f6 =
二. 结构数组
1. 使用直接引用方式定义结构
x.real = 0; % 创建字段(field)名为real,并为该字段赋值为0
x.imag = 0
x =
real: 0
imag: 0
然后可以将其动态扩充为数组:
x(2).real = 0; % 将x扩充为1×2的结构数组
x(2).imag = 0;
在任何需要的时候,也可以为数组动态扩充字段,如增加字段scale:
x(1).scale = 0;
这样,所有x都增加了一个scale字段,而x(1)之外的其他变量的scale字段为空:
x(1) % 查看结构数组的第一个元素的各个字段的内容
ans =
real: 0
imag: 0
scale: 0
x(2) % 查看结构数组的第二个元素的各个字段的内容,注意没有赋值的字段为空
ans =
real: 0
imag: 0
scale: []
clear x; x.real = [1 2 3 4 5]; x.imag = ones(10,10);
x(2).real = 'abc';
x(2).imag = rand(5,1);
x(3).real = x(1); x(3).imag = 3; x(3)
ans =
real: [1x1 struct]
imag: 3
下面看一个实际的例子来熟悉直接引用方式定义与显示结构。
【例】 温室数据(包括温室名、容量、温度、湿度等)的创建与显示。
(1) 直接对字段赋值法产生结构变量
green_house.name = '一号温室';
green_house.volume = '2000立方米'; % 创建温室容量字段
green_house.parameter.temperature = [31.2 30.4 31.6 28.7 29.7 31.1
30.9 29.6]; % 创建温室温度字段
green_house.parameter.humidity = [62.1 59.5 57.7 61.5 62.0 61.9
59.2 57.5]; % 创建温室湿度字段
(2)显示结构变量的内容
green_house % 显示结构变量结构
green_house =
name: '一号温室'
volume: '2000立方米'
parameter: [1x1 struct]
green_house.parameter % 用字段作用符号. 显示指定字段(parameter)中内容
ans =
temperature: [2x4 double]
humidity: [2x4 double]
green_house.parameter.temperature % 显示temperature字段中的内容
ans =
31.2000 30.4000 31.6000 28.7000
29.7000 31.1000 30.9000 29.6000
【例】在上例的基础上,创建结构数组用以保存一个温室群的数据。
green_house(2,3).name = '六号温室'; %产生2×3结构数组
green_house % 显示结构数组的结构
green_house =
2x3 struct array with fields:
name
volume
parameter
green_house(2,3) % 显示结构数组元素的结构
ans =
name: '六号温室'
volume: []
parameter: []
2. 使用struct函数创建结构
使用struct函数也可以创建结构,该函数产生或把其他形式的数据转换为结构数组。
struct的使用格式为:
s = sturct('field1',values1,'field2',values2,…);
s =
struct('type',{'big','little'},'color',{'blue','red'},'x',{3,4})
s =
1x2 struct array with fields:
type
color
x
s(1,1)
ans =
type: 'big'
color: 'blue'
x: 3
s(1,2)
ans =
type: 'little'
color: 'red'
x: 4
下面给出利用struct构建结构数组的具体实例。
【例】利用函数struct,建立温室群的数据库。
(1) struct预建立空结构数组方法之一
a = cell(2,3); %
创建2×3的单元数组
green_house_1=struct('name',a,'volume',a,'parameter',a(1,2))
green_house_1 =
2x3 struct array with fields:
name
volume
parameter
(2)struct预建空结构数组方法之二
green_house_2=struct('name',a,'volume',[],'parameter',[])
green_house_2 =
2x3 struct array with fields:
name
volume
parameter
(3)struct预建空结构数组方法之三
green_hopuse_3(2,3)=struct('name',[],'volume',[],'parameter',[])
green_hopuse_3 =
2x3 struct array with fields:
name
volume
parameter
(4)struct创建结构数组方法之四
a1={'六号房'};a2={'3200立方米'};
green_house_4(2,3)=struct('name',a1,'volume',a2,'parameter',[]);
T6=[31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6];
green_house_4
ans =
2x3 struct array with fields:
name
volume
parameter
2. 结构数组的操作
函数名
deal
fieldnames
getfield
rmfield
setfield
struct
struct2cell
isfield
isstruct
下面举一些具体的例子说明如果对结构数组加以操作。
【例】对结构数组进行字段的增添和删减操作。
(1)创建结构数组
clear,for k=1:10;department(k).number=['No.',int2str(k)];end
department
department =
1x10 struct array with fields:
number
(2)增添字段:在数组中任何一个结构上进行的字段增添操作,其影响遍及整个结构数组
department(1).teacher=40;department(1).student=300;
department(1).PC_computer=40;
department
department =
1x10 struct array with fields:
number
teacher
student
PC_computer
(3)增添子字段的操作只影响被操作的那个具体结构,而不是影响整个结构数组
department(2).teacher.male=35;department(2).teacher.female=13;
D2T=department(2).teacher
D1T=department(1).teacher
D2T =
male: 35
female: 13
D1T =
40
(4)删除子字段的操作也只影响被操作的那个具体结构
department(2).teacher=rmfield(department(2).teacher,'male');
department(2).teacher
ans =
female: 13
(5)删除字段的操作是对整个结构数组实施的
department=rmfield(department,'student') % 删除一个字段
department =
1x10 struct array with fields:
number
teacher
PC_computer
department=rmfield(department,{'teacher';'PC_computer'})%
删除2个字段
department =
1x10 struct array with fields:
number
【例】数值运算操作和函数在结构字段上的作用。
n_ex = 5; % 结构数组的长度
for k = 1:n_ex, % 创建1×5结构数组
ex(k).f = (k-1)*n_ex + [1:5];
end;
ex
ex =
1x5 struct array with fields:
f
%显示结构数组的字段中内容
disp([blanks(10) '结构字段中内容'])
for k=1:n_ex,disp(ex(k).f),end
结构字段中内容
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
class(ex(1).f)
ans =
double
% 对各结构字段中数值数组相应位置的数据相加求和
sum_f=zeros(1,5)
for k=1:n_ex,sum_f=sum_f+ex(k).f;end,sum_f
sum_f =
55 60 65 70 75
% 对结构数组字段中各元素分别求平方根
disp([blanks(20) 'ex.f的平方根值'])
for k=1:n_ex,
disp(sqrt(ex(k).f)),
end
ex.f的平方根值
1.0000 1.4142 1.7321 2.0000 2.2361
2.4495 2.6458 2.8284 3.0000 3.1623
3.3166 3.4641 3.6056 3.7417 3.8730
4.0000 4.1231 4.2426 4.3589 4.4721
4.5826 4.6904 4.7958 4.8990 5.0000
【例】 指令struct2cell和cell2struct的使用。
(1)创建“带2个字段的 结构数组”
for k=1:5,
ex(k).s=['No.' int2str(k)];
ex(k).f=(k-1)*5+[1:5];
end
(2)显示结构数组的内容
fprintf('%s\n','ex.s字段的内容 ');fprintf('%s\',blanks(4))
for k=1:5;fprintf('%s\\',[ex(k).s blanks(1)]);end
fprintf('%s\n',blanks(1)),fprintf('%s\n','ex.f字段的内容 ')
for k=1:5;disp(ex(k).f);end
ex.s字段的内容
No.1 \No.2 \No.3 \No.4 \No.5 \
ex.f字段的内容
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
(3)把ex结构数组转换为单元数组
C_ex=struct2cell(ex); % 带2个字段的(1×5)结构数组转换为(2×1×5)单元数组
size(C_ex)
fprintf('%s\',[C_ex{1,1,1},blanks(3)])
fprintf('%5g\',C_ex{2,1,1})
ans =
2 1 5
No.1 1 2 3 4 5
(4)把单元数组转换为结构数组之一
FS={'S_char';'F_num'};
EX1=cell2struct(C_ex,FS,1)
EX1 =
1x5 struct array with fields:
S_char
F_numric
EX1(1)
ans =
S_char: 'No.1'
F_numric: [1 2 3 4 5]
(5)把单元数组转换为结构数组之二
EX2=cell2struct(C_ex,'xx',2)
EX2 =
2x5 struct array with fields:
xx
(6)把单元数组转换为结构数组之三
YY=strvcat('y1','y2','y3','y4','y5');EX3=cell2struct(C_ex,YY,3)
EX3 =
2x1 struct array with fields:
y1
y2
y3
y4
y5
EX3(1)
ans =
y1: 'No.1'
y2: 'No.2'
y3: 'No.3'
y4: 'No.4'
y5: 'No.5'
EX3(2)
ans =
y1: [1 2 3 4 5]
y2: [6 7 8 9 10]
y3: [11 12 13 14 15]
y4: [16 17 18 19 20]
y5: [21 22 23 24 25]
From: