值分配给非标量结构的每个元素的同场 [英] Assign value to the same field of every element of non-scalar struct
问题描述
在MATLAB中,分配单元阵列到阵列结构领域富
是可能的。
In Matlab, assigning cell arrays to a struct arrays field foo
is possible with
my_array(1000).foo = [];
[my_array.foo] = some_cell{:};
现在我想要做的是一个单一的值赋给数组中的所有领域。但无论我试过,MATLAB会返回错误信息,而不是默默的假设,如果我要分配大小的单个元素 [1×1]
,它应该被分配到的所有的领域。我会很高兴,如果我可以简单地说,例如:
Now what I would like to do is assign a single value to all fields in the array. But whatever I tried, Matlab would return error messages instead of silently assuming that if I want to assign a single element of size [1x1]
, it should be assigned to all fields. I would be happy if I could simply say e.g.:
my_array.foo = pi;
??? Incorrect number of right hand side elements in dot name assignment.
Missing [] around left hand side is a likely cause.
所以,我怎么能一个值都在一个结构数组分配给字段?
So, how can I assign a single value to a field all over a struct array?
推荐答案
您可以使用 交易
来解决这个问题:
You can use deal
to solve this problem:
my_array(1000).foo = [];
[my_array.foo] =deal(pi);
不过,请注意方括号在第二行有必要的逗号分隔的列表 my_array.foo
暂时转换成向量。
这篇关于值分配给非标量结构的每个元素的同场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!