本文介绍了同时将值分配给多个结构字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个matlab结构,遵循以下模式:

I have a matlab structure that follows the following pattern:

S.field1.data1
          ...
 .field1.dataN
   ...
 .fieldM.data1
          ...
 .fieldM.dataN

我想同时为所有字段中的一个数据字段(例如data3)分配值.这在语义上类似于:

I would like to assign values to one data field (say, data3) from all fields simultaneously. That would be semantically similar to:

S.*.data3 = value

其中通配符"*"表示结构中的所有字段(field1,...,fieldM).这是可以在Matlab中不循环就能完成的事情吗?

Where the wildcard "*" represents all fields (field1,...,fieldM) in the structure. Is this something that can be done without a loop in matlab?

推荐答案

既然field1 .. fieldM是具有相同字段的结构数组,为什么不为"field"创建结构数组呢?然后,您可以使用deal轻松地将所有数据"成员设置为特定值.

Since field1 .. fieldM are structure arrays with identical fields, why not make a struct array for "field"? Then you can easily set all "data" members to a specific value using deal.

field(1).data1 = 1;
field(1).data2 = 2;
field(2).data1 = 3;
field(2).data2 = 4;

[field.data1] = deal(5);
disp([field.data1]);

这篇关于同时将值分配给多个结构字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:40
查看更多