How to create an empty struc with fields of a given struct?

555 ビュー (過去 30 日間)
Stefanie
Stefanie 2014 年 3 月 18 日
コメント済み: Shrinivas Iyengar 2022 年 3 月 14 日
I have a struct A with many fields:
A.a
A.b
...
A.z
Now I want to create a struct B, with the same fields:
B.a
B.b
...
B.z
With
B=struct(A)
B has also the same size as A, and all values of A are also included. But I want to have an empty struct with 0x1 dimension.
Background: in a loop I want to sort out some entries of A, that fit a certain criteria and write it into B. If I do
B=struct([])
B(n)=A(m)
then the structs do not match. Therefore I want to create B in advance and then assign it.
  1 件のコメント
Sara
Sara 2014 年 3 月 18 日
A is not an array, so I don't see how you can do B(n)=A(m). Do you mean you want to do something like: 1) B.f = A.a, or 2) B.a(n) = A.b(m)?

サインインしてコメントする。

採用された回答

James Tursa
James Tursa 2014 年 3 月 18 日
編集済み: James Tursa 2014 年 3 月 18 日
One way, which will give you a 0x0 struct, is:
f = fieldnames(A)';
f{2,1} = {};
B = struct(f{:});
Method basically obtained from this FEX submission by David Young:
CAUTION: From the description you give, it sounds like you will be dynamically increasing the size of B inside a loop. That will cause repeated copying that will slow down performance. If the number of elements of B turns out to be small, then no big deal. But if the number of elements of B turns out to be large, then you might experience a performance drag. In that case it might make sense to preallocate a size for B at the start (instead of 0x0) that is large enough to hold all the results, and then lop off the tail that you don't need after the loop is done.
  1 件のコメント
Shrinivas Iyengar
Shrinivas Iyengar 2022 年 3 月 14 日
What a beautiful solution! Thanks a lot 😀

サインインしてコメントする。

その他の回答 (3 件)

Stephen23
Stephen23 2019 年 5 月 23 日
編集済み: Stephen23 2019 年 5 月 23 日
The simplest and most efficient solution to the question posed is to just use indexing, e.g.:
>> A(1).x = 1;
>> A(1).y = 2;
>> A(2).x = 3;
>> A(2).y = 4
A =
1x2 struct array with fields:
x
y
>> B = A([])
B =
0x0 struct array with fields:
x
y
Using indexing we can also trivially obtain exactly the size requested in the original question:
>> B = A([],1)
B =
0x1 struct array with fields:
x
y
  3 件のコメント
Aryaman Mohapatra
Aryaman Mohapatra 2021 年 3 月 30 日
^
Emily T. Griffiths
Emily T. Griffiths 2021 年 10 月 14 日
Love the simplicity of this. Thanks!

サインインしてコメントする。


Ben Oeveren
Ben Oeveren 2018 年 8 月 17 日
編集済み: Ben Oeveren 2018 年 8 月 17 日
I often use
fields = {'field1','field2','field2'}
c = cell(length(fields),1);
s = cell2struct(c,fields);
  2 件のコメント
Donghoon Yeo
Donghoon Yeo 2019 年 5 月 23 日
編集済み: Donghoon Yeo 2019 年 5 月 23 日
Easy and perfectly works for me
KAE
KAE 2019 年 12 月 20 日
I think the line should be
fields = {'field1','field2','field3'}
So you get
s =
struct with fields:
field1: []
field2: []
field3: []

サインインしてコメントする。


Binxu
Binxu 2020 年 4 月 22 日
Actually I just fond a super simple way similar to Stephen Cobeldick 's answer
It seems to me that repmat is a super useful tool for initializing many data structure.
summary =
struct with fields:
anova_F: 1.0172
anova_p: 0.4279
anova_F_bsl: 1.0156
anova_p_bsl: 0.4335
t: 8.7424
t_p: 5.9749e-18
t_CI: [2×1 double]
stats = repmat(summary,0,0);
>> repmat(summary,0,0)
ans =
0×0 empty struct array with fields:
anova_F
anova_p
anova_F_bsl
anova_p_bsl
t
t_p
t_CI

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by