访问结构数据(matlab) [英] access struct data (matlab)

查看:120
本文介绍了访问结构数据(matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a= struct('a1',{1,2,3},'a2',{4,5,6})

如何获取1的值;

我尝试使用返回错误的a.a1 {1}

I try to use a.a1{1} which return errors

>> a.a1{1}
??? Field reference for multiple structure elements that is followed by more reference blocks is an
error.

如何访问1?谢谢.

修改 A = struct{'a1',[1 2 3],'a2',[4 5 6]}

如何访问1.我使用A(1).a1,但得到1 2 3

How can I access 1. I use A(1).a1 but I get 1 2 3

推荐答案

您必须改为:

a(1).a1

之所以如此,是因为用于创建结构的代码实际上创建了一个三元素结构数组,其中第一个数组元素包含a1: 1a2: 4,第二个数组元素包含a1: 2a2: 5 ,第三个数组元素包含a1: 3a2: 6.

The reason why is because the code you use to create your structure actually creates a 3-element structure array where the first array element contains a1: 1 and a2: 4, the second array element contains a1: 2 and a2: 5, and the third array element contains a1: 3 and a2: 6.

当您像以前一样在调用 STRUCT 时使用花括号{}时, MATLAB假定您要创建一个结构数组,在其中将单元格的内容分配到整个结构数组元素中.相反,如果您要创建一个单个的1比1结构元素,其中的字段包含单元格数组,则必须添加一组额外的花括号来包围您的单元格数组,如下所示:

When you use curly braces {} in a call to STRUCT like you did, MATLAB assumes you are wanting to create a structure array in which you distribute the contents of the cells across the structure array elements. If you instead want to create a single 1-by-1 structure element where the fields contain cell arrays, you have to add an additional set of curly braces enclosing your cell arrays, like so:

a = struct('a1',{{1,2,3}},'a2',{{4,5,6}});

然后您的原始a.a1{1}将可用.

如果您使用数字数组而不是单元格数组创建结构,就像这样:

If you create your structure using numeric arrays instead of cell arrays, like so:

A = struct('a1',[1 2 3],'a2',[4 5 6]);

然后您可以按以下方式访问值1:

Then you can access the value of 1 as follows:

A.a1(1)

有关在MATLAB中使用结构的更多信息,请查看此文档页面

For further information about working with structures in MATLAB, check out this documentation page.

这篇关于访问结构数据(matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆