How do I add a new field to an array of structures?

10 Ansichten (letzte 30 Tage)
Anna Lind
Anna Lind am 3 Jan. 2018
Kommentiert: Guillaume am 6 Mär. 2020
In my experiment I have one structure per subject I test. Each structure is quite complicated with many substructures e.g.:
sub.method1.result1
sub.method2.result1
sub.method2.result2
sub.method3.resulttype1.result1
sub.method2.resultstype2.result1 etc.
I have added all these structures together in an array of structures, lets call it aos, so that aos(1) will give me the structure for subject 1, aos(2) the structure for subject 2 etc. I then convert to tables to get a subset of the fields for all subjects to do my data analysis on. However, I now have an extra method that I would like to add to all the structures in the array. How can I do this? As I understand, all structures in an array of structures must have the same fields even though the fields do not need to contain the same type of data. So how do I add new fields to all the structures simultaneously in an array of structures?

Antworten (1)

KSSV
KSSV am 3 Jan. 2018
S = struct ;
S(1).name = 'Tom' ;
S(1).Age = 23 ;
S(1).sex = 'M' ;
S(2).name = 'Dick' ;
S(2).Age = 24 ;
S(2).sex = 'M' ;
S(3).name = 'Hary' ;
S(3).Age = 25 ;
S(3).sex = 'M' ;
% Add height to all structures arrays
C = num2cell([5 6 7]);
[S(:).height] = deal(C{:})
  5 Kommentare
Royi Avital
Royi Avital am 6 Mär. 2020
What about adding empty field (No values to it)?
Guillaume
Guillaume am 6 Mär. 2020
s(1).name.emptyfield = [];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Structures 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