玖叶教程网

前端编程开发入门

越览(47)——Matlab入门学习(4)之结构体数组

分享兴趣,传播快乐,

增长见闻,留下美好。

亲爱的您,这里是LearningYard学苑!

今天小编为大家带来文章

“越览(47)——Matlab入门学习(4)

之结构体数组。”

欢迎您的访问。

Share interest, spread happiness,

increase knowledge,and leave beautiful.

Dear, this is LearningYard Academy!

Today, the editor brings you the article

“Yue Lan (47)——Matlab introductory

learning (4): Structured array”

Welcome to visit!

一、内容摘要(Summary of Content)

本次推文将从内容摘要、思维导图、入门学习来介绍Matlab入门学习(4)之结构体数组。

This tweet will introduce the structured array of Matlab introductory learning (4) from content summary, mind map, and introductory learning.

二、思维导图(Mind Maping)

三、入门学习(introductory learning)

(一)基本概念(Basic concept)

在MATLAB 中,结构体数组是由多个结构体组成的数组。每个结构体可以包含不同类型和数量的字段,这些字段可以存储不同类型的数据,如数字、字符、向量、矩阵,甚至是另一个结构体。

In MATLAB, an array of structures is an array of multiple structures. Each structure can contain a different type and number of fields that can store different types of data, such as numbers, characters, vectors, matrices, or even another structure.

一个结构体类似于一个容器,能够以字段名的形式将不同的数据组织在一起。结构体数组允许创建多个这样的结构体并将它们按数组形式组织起来。这样,数组的每个元素都是一个结构体,并且所有结构体的字段名必须相同。

A structure is similar to a container and can organize different data together in the form of field names. Arrays of structures allow you to create multiple such structures and organize them as arrays. This way, each element of the array is a structure, and all structures must have the same field names.

(二)创建结构体数组(Create structure array)

在MATLAB 中,可以通过多种方式创建结构体数组。接下来介绍三种常见的方法。

In MATLAB, you can create an array of structures in several ways. Here are three common methods.

1. 直接构造(Direct construction)

可以直接定义一个结构体数组,并为每个元素设置字段值。示例代码如下所示:

You can directly define an array of structures and set field values for each element. The example code is as follows:

运行结果如下图所示:

The running result is shown as follows:

2. 使用结构体函数(Using structure functions)

struct 函数是用于创建结构体变量的一种方法,struct 函数的基本语法如下:S = struct (field1, val1, field2, val2, ...)。其中field1, field2, ... 是字段名称,可以是字符串或字符向量。val1, val2, ... 是对应的字段值,可以是任何 MATLAB 可以接受的数据类型。如果提供了多个字段和值,则 struct 函数将创建一个具有相应字段和值的单个结构体。示例代码如下:

The struct function is a method for creating structure variables. The basic syntax of the struct function is as follows: S = struct (field1, val1, field2, val2,...). Where field1, field2,... are field names, which can be strings or character vectors. Val1, val2,... are the corresponding field values, which can be any data type acceptable to MATLAB. If multiple fields and values are provided, the struct function will create a single structure with corresponding fields and values. The example code is as follows:

运行结果如下图所示:

The running result is shown as follows:

3. 使用 cell 转换(Using cell conversion)

首先可以先创建一个 cell 数组,然后将其转换成结构体数组。示例代码如下图所示:

First, you can create a cell array and then convert it to a structure array. The example code is shown below:

运行结果如下图所示:

The running result is shown as follows:

(三)增加字段(Add field)

假设为上文中出现的结构体数组 s中的每个结构体元素添加一个新的字段 gender,我们可以直接进行如下操作:

Assuming we add a new field gender for each struct element in the struct array's that appears above, we can do the following directly:

运行结果如下图所示:

The running result is shown as follows:

(四)删除字段(Delete field)

删除结构体中的字段可以通过两种主要方法来实现:一种是直接将该字段设置为 [](空矩阵),另一种是使用 rmfield 函数。

Removing a field from a struct can be achieved in two main ways: by directly setting the field to [] (an empty matrix), or by using the rmfield function.

首先构建一个新的结构体数组s3,通过空矩阵删除结构体中的 job 字段的代码如下:

First, build a new structure array s3. The code to delete the job field in the structure through the empty matrix is as follows:

这样,job 字段就被正确地删除了,并且结构体数组的每个元素的 job 字段都被设置为空矩阵 []。

This way, the job field is deleted correctly, and the job field of each element of the structure array is set to the empty matrix [].

如果你需要删除多个字段,可以将字段名称放在一个元胞数组中,并传递给 rmfield 函数。示例代码如下图所示:

If you need to delete multiple fields, you can put the field names in a cell array and pass them to the rmfield function. The example code is shown below:

运行结果如下图所示:

The running result is shown as follows:

今天的分享就到这里了。

如果您对文章有独特的想法,

欢迎给我们留言,让我们相约明天。

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

文案|yyz

排版|yyz

审核|hzy

翻译:火山翻译

参考资料:Chat GPT、哔哩哔哩

本文由LearningYard学苑整理发出,如有侵权请在后台留言!

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言