Adding a new field to a struct array
이전 댓글 표시
Hey All! I'm trying to add a new field to a struct array inline. I have the following struct:
A(1).name = 'bob';
A(2).name = 'cindy';
newstruct = 'address'
How do I go about adding the a field A.address to the array struct with the field name being define din the newstruct variable? The existing array should then look like the following:
A(1).name = 'bob';
A(2).name = 'cindy';
A(1).address = [];
A(2).address = [];
채택된 답변
추가 답변 (2개)
Siva
2018년 1월 27일
1 개 추천
A(1).name = 'bob'; A(2).name = 'cindy'; A(1).address = ' Flat No.502, Apple Towers,... 1st line, Ravindra Nagar, Guntur,Andhra Pradesh, India,PIN-522006'; A(2).address = ' Flat No.102, Balaji Towers,... 2nd line, Brodipet, Guntur, Andhra Pradesh, India, PIN-522006';
sarah wentzel
2022년 1월 23일
편집: sarah wentzel
2022년 1월 23일
1 개 추천
I created this cell
somecell = struct('Name', {'bob', 'lea', 'pat'}, 'Birth_year', {1990, 1988, 2021})
and now I want to add new information, favorite color. I know I can do it one by one like this:
somecell(1).color = 'blue'
somecell(2).color = 'red'
somecell(3).color = 'green'
But is there a way to do it with only one line? something like;
somecell (:,3).color = {'blue', 'red', 'green'}
thank you!
댓글 수: 1
S = struct('Name', {'bob', 'lea', 'pat'}, 'Birth_year', {1990, 1988, 2021})
[S.color] = deal('blue','red','green')
Or from a cell array:
C = {'small', 'large', 'medium'};
[S.tshirtsize] = C{:}
카테고리
도움말 센터 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!