Adding a new field to a struct array

조회 수: 1,691 (최근 30일)
C Delog
C Delog 2018년 1월 26일
댓글: Walter Roberson 2022년 6월 11일
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 = [];

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 27일
A(1).(newstruct) = value;
If you have a cell array of contents to be put into existing entries, then you can use
[A.(newstruct)] = values_cell{:};
to set all of them at the same time.
  댓글 수: 2
sarah wentzel
sarah wentzel 2022년 1월 23일
what are value in the first option and values_cell in the second? are those new variables?
Walter Roberson
Walter Roberson 2022년 6월 11일
value in the above could be any variable or expression of any size or data type.
values_cell in the above would have to be a variable containing a cell array, in which the number of entries was the same as numel(A); each cell entry could be any data type or size.
More obscurely, values_cell could instead be the name of a true function (not function handle) for a function that could be called with no parameters, and which returned a cell array.
More obscurely still, values_cell could be the name of a class with a class constructor that could be invoked with no parameters, and the class defined subsref with {} operation, or defined the specialized method for overloading {}, so that the class name with no parameters defines an object and the {:} invoked whatever the {} operation was redefined as.

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

추가 답변 (2개)

Kallam Haranadha Reddy
Kallam Haranadha Reddy 2018년 1월 27일
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
sarah wentzel 2022년 1월 23일
편집: sarah wentzel 2022년 1월 23일
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
Stephen23
Stephen23 2022년 1월 23일
편집: Stephen23 2022년 1월 23일
S = struct('Name', {'bob', 'lea', 'pat'}, 'Birth_year', {1990, 1988, 2021})
S = 1×3 struct array with fields:
Name Birth_year
[S.color] = deal('blue','red','green')
S = 1×3 struct array with fields:
Name Birth_year color
Or from a cell array:
C = {'small', 'large', 'medium'};
[S.tshirtsize] = C{:}
S = 1×3 struct array with fields:
Name Birth_year color tshirtsize

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

카테고리

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