Matlab-结构体数组的索引

版本:matlab2014a(如果运行有错误,可以对应版本的help进行修正。这里是我当时总结的笔记,供大家参考,具体查阅相应版本的help)

%% 结构体数组的索引 (Structure Array)
% 注意:结构体的创建 与 矩阵/元胞数组 不同,元胞数组以元胞的方式进行储存,而结构体是以 fields 储存的(而不是以对象个数储存的)
% 所以元胞数组创建时,提供元胞的大小,而结构体的创建
%% 1.n * 1 的结构体数组
imname = dir(['C:\Users\ncf\Desktop\' '*.doc'])
imname.name % 返回所有的 name ,name 相当于一个结构体的变量,一个属性(注意这个和 cell 的联系)
imname(:).name %返回所有的 name
imname(1).name % 返回第一个对象的 name 
imname(1) % 返回第一个对象的所有属性2
%% 2.使用 [] 连接结构体数组
% To concatenate structures, they must have the same set of fields, 
% but the fields do not need to contain the same sizes or types of data.
% 前提结构体必须要有相同 set of fields 属性集,但是对于某一个属性不需要包含相同大小和类型的数据
struct1.a = 'first';
struct1.b = [1,2,3];
struct2.a = 'second';
struct2.b = rand(5);
combined = [struct1, struct2] % combined 为 1*2 的 struct
%% 3.create a 2-by-2 struct array 创建一个 2*2 的结构体数组
new(1,1).a = 1;
new(1,1).b = 10;
new(1,2).a = 2;
new(1,2).b = 20;
new(2,1).a = 3;
new(2,1).b = 30;
new(2,2).a = 4;
new(2,2).b = 40;
% 理解 a b 相当于结构体的属性,每一个对象都有 a b 属性
% 而对象的索引就是 new(2,2) :第四个对象,第二行第二列的对象,这种可以理解成将矩阵一样,实际不是以二维储存的,而是 new(3)
% 将二维转成列 
larger = [combined; new] % 因为 combined new 都含有 a b 属性则可以合并
% [] 可以连接矩阵也可以连接结构体
% [ ; ] 竖直连接 相当于函数 vertcat
% [   ] 水平连接 相当于函数 horzcat 

% % % % n * n 结构体的初始化和创建 (与矩阵,元胞数组的初始化不同)
newStruct(1:25,1:50) = struct('a',ones(20),'b',zeros(30),'c',rand(40)); % 指定初始化值
% or 
newStruct(1:25,1:50).a = ones(20); % 指定初始化值
newStruct(1:25,1:50).b = zeros(30);
newStruct(1:25,1:50).c = rand(40);
% or
newStruct(1:25,1:50).a = []; % 不指定初始化值
newStruct(1:25,1:50).b = [];
newStruct(1:25,1:50).c = [];
% or
newStruct(1:25,1:50) = struct('a',[],'b',[],'c',[]); % 不指定初始化值

%% 4.结构体的储存方式
% A structure is a data type that groups related data using data containers called fields. 
% Each field can contain data of any type or size.
% 结构体使用 fields 这样的数据容器将 结构体与数据联系在一起,每一个 field 可以包含不同大小数据类型的数据
%% 5.结构体数组(由每一个结构体组成(即对象),每一个结构体(对象)有相同的属性集(名字和数量相同) fields,不同结构体里的相同属性值,数据类型/大小可以不同)
% /1.All structs in the array have the same number of fields
% /2.All structs have the same field names
% /3.Fields of the same name in different structs can contain different types or sizes of data.
patient(1).name = 'John Doe';
patient(1).billing = 127.00;
patient(1).test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205];
patient(3).name = 'New Name';
patient(3)
% 对于 patient(3) 对象的 billing test 未指定时为空
%% 6.Comma-Separated Lists 
% 1.Comma-Separated List
% Typing in a series of numbers separated by commas gives you what is called a comma-separated list
1,2,3 % 用,分开

% 2.Generating a Comma-Separated List:

% 2/1.Generating a List from a Cell Array (由元胞数组产生)
C = cell(4,6);
for k = 1:24
    C{k} = k*2;
end
C
C{:,5} % extracting the fifth column generates the following comma-separated lis
% C{1,5},C{2,5},C{3,5},C{4,5} 上面的那个相当于这个

% 2/2.Generating a List from a Structure (由结构数组生成)
S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
S.f5
% S(1).f5,S(2).f5,S(3).f5,S(4).f5 上面的与这个相同

% 2/3.Assigning Output from a Comma-Separated List (指定输出)
C = cell(4,6);
for k = 1:24
    C{k} = k*2;
end
[c1,c2,c3,c4,c5,c6] = C{1,1:6};
c5
% 2/3/1.对于指定少的输出时,舍弃其他的,对于结构体也一样
% You also can use the deal function for this purpose 也可以使用函数 deal
[c1,c2,c3] = C{1,1:6}

S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2);
[sf1,sf2,sf3] = S.f5;
sf3

% 3.使用 Comma-Separated Lists 
% 3/1.Constructing Arrays (重建数组)
% When you specify a list of elements with C{:, 5}, 
% MATLAB inserts the four individual elements
A = {'Hello',C{:,5},magic(4)}
% 3/2.Displaying Arrays (显示数组0)
% Use a list to display all or part of a structure or cell array
A{:}
% 3/3.Concatenation (合并)
% 提取出数据再合并
A = [C{:,5:6}]
% 3/4.Function Call Arguments (作为函数的输入)
X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));
C = cell(2,3);
C{1,1} = 'LineWidth';
C{2,1} = 2;
C{1,2} = 'MarkerEdgeColor';
C{2,2} = 'k';
C{1,3} = 'MarkerFaceColor';
C{2,3} = 'g';
figure
plot(X,Y,'--rs',C{:})
% 3/5.Function Return Values (函数返回值) 
% These values are returned in a list with each value separated by a comma
% Instead of listing each return value, you can use a comma-separated list with a structure or cell array
% 函数的返回值在返回时,是有逗号隔开的,可以用 a comma-separated list with a structure or cell
% array 代替列出每一个返回值
C = cell(1,3);
[C{:}] = fileparts('work/mytests/strArrays.mat')
%% 7.Access Elements of a Nonscalar Struct Array (从结构数组中获取元素,与之前的内容有交集)
% 1.access data from multiple elements of a nonscalar structure array (获取数据)
s(1).f = 1;
s(2).f = 'two';
s(3).f = 3 * ones(3);
s(1:3).f
% or
s.f 
% MATLAB? returns the data from the elements in a comma-separated list,
% 你不能指定这个 list 到 一个变量中  syntax v = s.f (错,这个默认获取第一个数据,舍弃后两个)
% 可以采取以下两种方式:
[v1, v2, v3] = s.f % 指定 list 到等个数的变量中
c = {s.f}; % 指定 list 到元胞数组中
% 2.process data from multiple elements of a nonscalar structure array (处理数据)
% 如果你想对数组中的每一个元素(具体到每一个结构体的某一属性的元素)施以相同处理,使用 arrayfun 函数
numElements = arrayfun(@(x) numel(x.f), s)
% 数出每一个结构体中的 f 属性元素数目,相当于下面的表达式(这样可以扩展函数的使用范围,使输入的可以是很多类型的数据)
[numel(s(1).f) numel(s(2).f) numel(s(2).f)]
%% 8.Ways to Organize Data in Structure Arrays (组织数据的方法)
% 1.Plane Organization 
img.red = RED;
img.green = GREEN;
img.blue = BLUE;
adjustedRed = .9 * img.red; % 对某一属性的面操作
% 2.Element-by-Element Organization 
patient(1).name = 'John Doe';
patient(1).billing = 127.00;
patient(1).test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205]; % 对某一对象操作
%% 9.Memory Requirements for a Structure Array (结构体的内存分配)
% Structure arrays do not require completely contiguous memory. 
% However, each field requires contiguous memory, 
% as does the header that MATLAB? creates to describe the array.
% For very large arrays, incrementally increasing the number of fields or the number of elements in a field results in Out of Memory errors.
% However, in this case, MATLAB only allocates memory for the header, and not for the contents of the array.
% matlab 只为 field(the header) 分配内存,而不是为数组中的内容
% Structure arrays 不需要连续的内存,但是每一个 field 需要连续的内存
%% 10.Access Data in Nested Structures (嵌套结构体)
% syntax :
% structName(index).nestedStructName(index).fieldName(indices)
% structName(index) 是最大的类 ,nestedStructName(index)
% 是较小的类(生成对象),fieldName(indices) 这个索引的是具体结构体的属性的内容
s.n.a = ones(3);
s.n.b = eye(4);
s.n.c = magic(5);
s(1).n(2).a = 2*ones(3);
s(1).n(2).b = 2*eye(4);
s(1).n(2).c = 2*magic(5);

s(2).n(1).a = '1a';
s(2).n(2).a = '2a';
s(2).n(1).b = '1b';
s(2).n(2).b = '2b';
s(2).n(1).c = '1c';
s(2).n(2).c = '2c';
%  s(2).n(2).c :s 的第二个类下的 n 类的 第二个对象的 c 属性
% Access part of the array in field b of the second element in n within the first element of s:
part_two_eye = s(1).n(2).b(1:2,1:2)

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nachifur

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值