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

10 views (last 30 days)
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?

Answers (1)

KSSV
KSSV on 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 Comments
Royi Avital
Royi Avital on 6 Mar 2020
What about adding empty field (No values to it)?

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!