索引嵌套结构中的所有值 [英] Indexing over all values in nested struct

查看:73
本文介绍了索引嵌套结构中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的结构,其中包含值,并定义为:

I have a nested struct which contains values and is defined as:

mystruct.level1.a = 1;
mystruct.level1.b = 2;
mystruct.level2.a = 8;
mystruct.level2.b = 9;

我要对级别1和级别2中的元素执行操作.我想做的是访问level1和level2中的值,将它们放入向量中,而不引用嵌套的字段名称.

I want to perform operations on the elements in level1 and level2. What I want to do is access the values in level1 and level2, put them in a vector, without referencing the nested field names.

例如我想做类似的事情:

E.g. I'd like to do something like:

level1_vector = [mystruct.level1]

我想输出以下内容:

level1_vector = [1 2]

我该怎么办?

推荐答案

使用以下两个功能的组合:

Use the combination of two functions below:

cell2mat(struct2cell(mystruct.level1))

这篇关于索引嵌套结构中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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