Filter löschen
Filter löschen

How to Access to Fields of a Non-scalar Struct?

3 Ansichten (letzte 30 Tage)
Rightia Rollmann
Rightia Rollmann am 12 Mär. 2017
Kommentiert: Walter Roberson am 12 Mär. 2017
How can I create the 4-by-1 cell array D from the struct A?
A(1).B.C = 'a';
A(2).B.C = 'b';
A(3).B.C = 'c';
A(4).B.C = 'd';
D =
'a'
'b'
'c'
'd'

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Mär. 2017
D = arrayfun(@(IDX) A(IDX).B.C, (1:numel(A)).', 'Uniform', 0);
This is the safest approach, as it will work for the case where there are fields in addition to B within A, where some other approaches would not.
But in the specialized case where B is the only field inside A, then
temp = cell2mat(struct2cell(A));
D = {temp.C}.';
  2 Kommentare
Rightia Rollmann
Rightia Rollmann am 12 Mär. 2017
Thanks!
What does
.'
do for the code? I know ' operator, but I don't know .' operator.
Walter Roberson
Walter Roberson am 12 Mär. 2017
.' is plain transpose. ' is conjugate transpose.
For arrays that are not numeric arrays, they come out the same, but I prefer to write .' to remove any ambiguity about whether I intend the transpose to be conjugate or not.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by