当前位置:网站首页>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 :
边栏推荐
- 1 plug-in to handle advertisements in web pages
- 【yolov3损失函数】
- Pytorch softmax regression
- Acid transaction theory
- Seven ways to achieve vertical centering
- 1.php的laravel创建项目
- Halcon template matching actual code (I)
- mmclassification 训练自定义数据
- Wireless WiFi learning 8-channel transmitting remote control module
- [upsampling method opencv interpolation]
猜你喜欢

13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system

【主流Nivida显卡深度学习/强化学习/AI算力汇总】

Riddle 1

Redis master-slave mode

【无标题】

《增长黑客》阅读笔记
![[pytorch modifies the pre training model: there is little difference between the measured loading pre training model and the random initialization of the model]](/img/ad/b96e9319212cf2724e0a640109665d.png)
[pytorch modifies the pre training model: there is little difference between the measured loading pre training model and the random initialization of the model]

Intern position selection and simplified career development planning in Internet companies

【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】

Error modulenotfounderror: no module named 'cv2 aruco‘
随机推荐
Codeworks 5 questions per day (1700 average) - day 5
12. (map data) cesium city building map
Implementation of array hash function in PHP
【无标题】
简单解决redis cluster中从节点读取不了数据(error) MOVED
Pytorch softmax regression
Matlab imoverlay function (burn binary mask into two-dimensional image)
How to get a token from tokenstream based on Lucene 3.5.0
[calculation of loss in yolov3]
JS for循环 循环次数异常
1个插件搞定网页中的广告
【主流Nivida显卡深度学习/强化学习/AI算力汇总】
Yolov5 target detection neural network -- calculation principle of loss function
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
Unity xlua monoproxy mono proxy class
Wireless WiFi learning 8-channel transmitting remote control module
Splunk configuration 163 mailbox alarm
Complete activity switching according to sliding
【上采样方式-OpenCV插值】
Riddle 1