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

조회 수: 10 (최근 30일)
Anna Lind
Anna Lind 2018년 1월 3일
댓글: Guillaume 2020년 3월 6일
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?

답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by