为非标量结构的每个元素的相同字段赋值 [英] Assign value to the same field of every element of non-scalar struct

查看:35
本文介绍了为非标量结构的每个元素的相同字段赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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 都会返回错误消息,而不是默默地假设如果我想分配一个大小为 [1x1] 的元素,它应该分配给 all 字段.如果我可以简单地说例如:

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?

推荐答案

您可以使用 deal 解决这个问题:

You can use deal to solve this problem:

my_array(1000).foo = [];
[my_array.foo] =deal(pi);

但是,请注意第二行中的方括号是将逗号分隔列表 my_array.foo 临时转换为向量所必需的.

However, note the square brackets in the second line with are necessary to temporarily convert the comma separated list my_array.foo into a vector.

这篇关于为非标量结构的每个元素的相同字段赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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