当前位置:网站首页>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 :
边栏推荐
- 【L1、L2、smooth L1三类损失函数】
- The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
- Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
- 一次生产环境redis内存占用居高不下问题排查
- pytorch-softmax回归
- [configuration method of win11 multi-user simultaneous login remote desktop]
- 【PyTorch预训练模型修改、增删特定层】
- Yolov5 target detection neural network -- calculation principle of loss function
- Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
- 简单解决redis cluster中从节点读取不了数据(error) MOVED
猜你喜欢

Hiengine: comparable to the local cloud native memory database engine

Mmclassification training custom data

Multi table operation - sub query

Seven ways to achieve vertical centering

Intern position selection and simplified career development planning in Internet companies

pytorch-多层感知机MLP

Error modulenotfounderror: no module named 'cv2 aruco‘

全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
![[calculation of loss in yolov3]](/img/8c/1ad99b8fc1c5490f70dc81e1e5c27e.png)
[calculation of loss in yolov3]

Principle of persistence mechanism of redis
随机推荐
Uniapp + unicloud + Unipay realize wechat applet payment function
Splunk configuration 163 mailbox alarm
如何让你的产品越贵越好卖
13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
[untitled]
Redis集群(主从)脑裂及解决方案
Mongodb replica set
MySQL statistical skills: on duplicate key update usage
你做自动化测试为什么总是失败?
调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
《增长黑客》阅读笔记
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
Multi table operation - Auto Association query
【PyTorch预训练模型修改、增删特定层】
redis集群中hash tag 使用
What is the difference between canvas and SVG?
pytorch-多层感知机MLP
Pytorch MLP
871. Minimum Number of Refueling Stops
1 plug-in to handle advertisements in web pages