如何将struct数组传递给另一个struct数组?

时间:2022-09-06 11:27:12

I am trying to pass a struct array to another without success.

我试图将结构数组传递给另一个没有成功。

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

q(1).n = 'nameA';
q(2).n = 'nameB';
q(3).n = 'nameC';
q(3).f = [];

q.f = s.f

The field n shouldn't be modified.

不应修改字段n。

Do I miss something?

我错过了什么吗?

4 个解决方案

#1


2  

You can assign each field of an array of structs directly to a cell array and then you can use deal to convert a struct to a cell array:

您可以将结构数组的每个字段直接分配给单元数组,然后您可以使用deal将结构转换为单元数组:

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

q(1).n = 'nameA';
q(2).n = 'nameB';
q(3).n = 'nameC';

c = cell(3,1);
[c{:}] = deal(s.f);
[q.f] = c{:};

Here is a good article on this sort of thing

这是一篇关于此类事情的好文章

Edit: Or as Shai points out you can just go

编辑:或者正如Shai指出你可以去

[q.f] = s.f

#2


2  

How about arrayfun

如何使用arrayfun

q = arrayfun( @(x,y) setfield( x, 'f', y.f ), q, s );

Apparently setfield is only for setting one struct element in struct array -- thus the arrayfun.

显然,setfield仅用于在struct数组中设置一个struct元素 - 因此是arrayfun。

EDIT:
a much better answer given by Dan.

编辑:Dan给出的更好的答案。

#3


1  

I wanted to make this suggestion by Shai more visible because it’s easier to read.

我想让Shai的这个建议更加明显,因为它更容易阅读。

[q.f] = s.f

[q.f] = s.f.

#4


1  

Though I'd say @Dan's answer is pretty canon for this question, I'd like to present an alternative:

虽然我会说@Dan的答案对于这个问题很有道理,但我想提出一个替代方案:

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

[q(1:length(s)).f] = s.f;

Though slightly more verbose than the [q.f] = s.f syntax, it has the advantage of functioning as expected even if q has not been preallocated to the correct size to be a copy of s.

虽然比[q.f] = s.f语法稍微冗长,但它具有按预期运行的优点,即使q尚未预先分配到正确的大小以作为s的副本。

For example:

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

[q.f] = s.f;

Returns q.f as a 1x1 struct equal to s(1).f

将q.f作为1x1结构返回,等于s(1).f

#1


2  

You can assign each field of an array of structs directly to a cell array and then you can use deal to convert a struct to a cell array:

您可以将结构数组的每个字段直接分配给单元数组,然后您可以使用deal将结构转换为单元数组:

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

q(1).n = 'nameA';
q(2).n = 'nameB';
q(3).n = 'nameC';

c = cell(3,1);
[c{:}] = deal(s.f);
[q.f] = c{:};

Here is a good article on this sort of thing

这是一篇关于此类事情的好文章

Edit: Or as Shai points out you can just go

编辑:或者正如Shai指出你可以去

[q.f] = s.f

#2


2  

How about arrayfun

如何使用arrayfun

q = arrayfun( @(x,y) setfield( x, 'f', y.f ), q, s );

Apparently setfield is only for setting one struct element in struct array -- thus the arrayfun.

显然,setfield仅用于在struct数组中设置一个struct元素 - 因此是arrayfun。

EDIT:
a much better answer given by Dan.

编辑:Dan给出的更好的答案。

#3


1  

I wanted to make this suggestion by Shai more visible because it’s easier to read.

我想让Shai的这个建议更加明显,因为它更容易阅读。

[q.f] = s.f

[q.f] = s.f.

#4


1  

Though I'd say @Dan's answer is pretty canon for this question, I'd like to present an alternative:

虽然我会说@Dan的答案对于这个问题很有道理,但我想提出一个替代方案:

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

[q(1:length(s)).f] = s.f;

Though slightly more verbose than the [q.f] = s.f syntax, it has the advantage of functioning as expected even if q has not been preallocated to the correct size to be a copy of s.

虽然比[q.f] = s.f语法稍微冗长,但它具有按预期运行的优点,即使q尚未预先分配到正确的大小以作为s的副本。

For example:

s(1).f = (1:3);
s(2).f = (4:6);
s(3).f = (7:9);

[q.f] = s.f;

Returns q.f as a 1x1 struct equal to s(1).f

将q.f作为1x1结构返回,等于s(1).f