当前位置:网站首页>Matlab struct function (structure array)
Matlab struct function (structure array)
2022-07-05 11:58:00 【**inevitable**】
List of articles
grammar
s = struct
s = struct(field,value)
s = struct(field1,value1,...,fieldN,valueN)
s = struct([])
s = struct(obj)
explain
s = struct % Create a scalar without any fields (1×1) Structure .
s = struct(field,value)
% Create an array of structures with specified fields and values .
%value The input parameter can be any data type , For example, numerical value 、 Boolean value 、 Character or cell array
% If value Not a cellular array , perhaps value Is a scalar cell array , be s Is a scalar structure
% for example ,s = struct('a',[1 2 3]) Create a 1×1 Structure , among s.a = [1 2 3]
% If value Is a non scalar cell array , be s Is the dimension and value The same structure array .s Each element of contains value The corresponding element of
% for example ,s = struct('x',{'a','b'},'y','c') return s(1).x = 'a'、s(2).x = 'b'、s(1).y = 'c' and s(2).y = 'c'
% If value Is an empty cell array {}, be s It's empty (0×0) Structure
s = struct(field1,value1,...,fieldN,valueN) Create multiple fields
% aggregate value1,...,valueN Any non scalar cell array in must have the same dimension
% If value All inputs of are not cell arrays , perhaps value All inputs as cell arrays in are scalars , be s Is a scalar structure
% If all value All inputs are non scalar cell arrays , be s Has the same dimension as a non scalar cell array
% For any array whose type is scalar cell array or any other data type value,struct take value Insert the content of s In the relevant fields of all elements of
% If anything value The input is an empty cell array {}, The output s It's empty (0×0) Structure
% To specify an empty field and keep the values of other fields , Please switch to [] As value Input
s = struct([]) Create an empty without any fields (0×0) Structure
s = struct(obj) Create include and obj The scalar structure of the field name and value corresponding to the attribute of
%struct Functions do not convert obj, It's going to be s Create a new structure . This structure does not retain class information , So dedicated 、 Protected and hidden properties in s Become a public field
%struct Function will warn you when you use this syntax
Input parameters
filed : Field name
Field name , Specify as character vector or string scalar . Valid field names begin with a letter , It can contain letters 、 Numbers and underscores . The maximum length of a field name is namelengthmax Value returned by function
value: value
value , Specify as an array of any type . If value Any input is a non scalar cell array , Then all non scalar cell array inputs must have the same dimension . If value Any input is an empty cell array {}, The output is an empty structure array . To specify a single empty field , Please use [].
obj: object
object , Specify as a scalar or an array of objects .struct Function will obj The attribute of is copied into the field of the new scalar structure .struct Functions cannot create structures based on most basic data types . for example , If obj have double or char data type , be struct An error message will be sent . however ,struct The attributes of the table or schedule will be returned as a structure .
Example
example 1: Store relevant data variables in the structure
% Create a structure by adding fields using dot notation
data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
The results are shown below :
% Draw sine wave , Here, reference by field name x and y Array of values , Then add the title
plot(data.x,data.y)
title(data.title)
The results are shown below :
example 2: A structure with one field
% Create a non scalar structure containing a field
field = 'f';
value = {'some text';
[10, 20, 30];
magic(5)};
s = struct(field,value)
% View the content of each element
s.f
The results are shown below :
% Accessing fields of non scalar structures ( for example s.f) when ,MATLAB Return a comma separated list
% In this example ,s.f Equivalent to s(1).f, s(2).f, s(3).f

example 3: A structure with multiple fields
field1 = 'f1'; value1 = zeros(1,10);
field2 = 'f2'; value2 = {'a', 'b'};
field3 = 'f3'; value3 = {pi, pi.^2};
field4 = 'f4'; value4 = {'fourth'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
%value2 and value3 The cell array of is 1×2 Array , therefore s It's also 1×2 Array
% because value1 It's a numerical array, not a cellular array , therefore s(1).f1 and s(2).f1 Have the same content
% Similarly , because value4 The cell array of has a single element , therefore s(1).f4 and s(2).f4 Have the same content
The results are shown below :
example 4: Structure with empty fields
% Create a structure containing an empty field . Use [] Specify the value of the empty field
s = struct('f1','a','f2',[])
The results are shown below :
example 5: Fields containing cell arrays
field = 'mycell';
value = {
{'a','b','c'}};
s = struct(field,value)
The results are shown below :
example 6: Empty structure
% Create an empty structure with multiple fields
s = struct('a',{},'b',{},'c',{})
The results are shown below :
example 7: Nested structure
% Create a nested structure , among a Is a structure with a field , This field contains another structure
a.b = struct('c',{},'d',{})
% see a.b The name of the field
fieldnames(a.b)
The results are shown below :
边栏推荐
- Yolov5 target detection neural network -- calculation principle of loss function
- XML解析
- Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
- 【 YOLOv3中Loss部分计算】
- 一次生产环境redis内存占用居高不下问题排查
- Empêcher le navigateur de reculer
- MVVM framework part I lifecycle
- Pytorch weight decay and dropout
- Shell script file traversal STR to array string splicing
- Application of a class of identities (vandermond convolution and hypergeometric functions)
猜你喜欢
随机推荐
yolov5目標檢測神經網絡——損失函數計算原理
redis主从模式
调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
C operation XML file
[cloud native | kubernetes] actual battle of ingress case (13)
What is the difference between canvas and SVG?
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
Empêcher le navigateur de reculer
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
Install esxi 6.0 interactively
pytorch-权重衰退(weight decay)和丢弃法(dropout)
【 YOLOv3中Loss部分计算】
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
redis集群中hash tag 使用
mmclassification 训练自定义数据
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
【L1、L2、smooth L1三类损失函数】
【云原生 | Kubernetes篇】Ingress案例实战(十三)
【Win11 多用户同时登录远程桌面配置方法】

![[untitled]](/img/56/6a9a4bcab6503872942fff7a365def.jpg)







