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

16 visualizaciones (últimos 30 días)
Anna Lind
Anna Lind el 3 de En. de 2018
Comentada: Guillaume el 6 de Mzo. de 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?

Respuestas (1)

KSSV
KSSV el 3 de En. de 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 comentarios
Royi Avital
Royi Avital el 6 de Mzo. de 2020
What about adding empty field (No values to it)?
Guillaume
Guillaume el 6 de Mzo. de 2020
s(1).name.emptyfield = [];

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by