matlab basic data structure struct

The evolution together a basic data structure struct calculated -matlab

Find useful, welcome to discuss mutual learning together - Follow Me

Reference
http://blog.sina.com.cn/s/blog_468651400100c6c0.html

Structure array struct

  • MATLAB provides two definitions of the structure: Direct application and use struct function

    Direct reference definition structure

  • And establish numeric arrays, objects do not need to create a new struct declared in advance, it can be referenced directly, and can be dynamically expanded. For example, the establishment of a complex variable x
x.real = 0; % 创建字段名为real,并为该字段赋值为0
x.imag = 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: []
  • It should be noted, real x's, imag, scale field may not be a single data element, which may be any type of data, it may be a vector, array, matrix or even other variables cellular array structure, and its different fields data type need not be identical. E.g:
clear x; x.real = [1 2 3 4 5]; x.imag = ones(10,10);
  • An array of the same field data type does not require different elements of the same
x(2).real = '123';
x(2).imag = rand(5,1);
  • Structure can even be defined by a field of data type array reference field
x(3).real = x(1); x(3).imag = 3; x(3)
ans =
real: [1x1 struct]
imag: 3

Create a structure using the struct function

  • Use can also create structure struct function, the function generated or converted to other forms of array data structures.
  • struct use the format:
    S = sturct ( 'field1', values1, 'Field2', values2, ...);
  • This function will generate an array of structures specifying field names and corresponding data with the data it contains values1, valuese2 the like must be data having the same dimensions, the storage of data in correspondence with the structure location. For the assignment of struct uses cellular array. Array values1, values2 like may be cellular array, cellular scalar values ​​or a single unit. Values ​​of each data field is assigned to the respective fields.
  • When valuesx of cellular array, the same dimension and the dimension of the array generated Cellular array of structures. And when the data does not include cellular, the dimension of the array structure is obtained in the 1 × 1.
s = struct('type',{'big','little'},'color',{'blue','red'},'x',{3,4})
s =
1x2 struct array with fields:
type
color
x
% 得到维数为1×2的结构数组s,包含了type、color和x共3个字段。这是因为在struct函数中{'big','little'}、{'blue','red'}和{3,4}都是1×2的元胞数组,可以看到两个数据成分分别为:
s(1,1)
ans =
type: 'big'
color: 'blue'
x: 3
   s(1,2)
ans =
type: 'little'
color: 'red'
x: 4
% 相应的,如果将struct函数写成下面的形式:
s = struct('type',{'big';'little'},'color',{'blue';'red'},'x',{3;4})
s =
2x1 struct array with fields:
type
color
x
% 则会得到一个2×1的结构数组。

Guess you like

Origin www.cnblogs.com/cloud-ken/p/11268543.html