MATLAB Answers

Adding a new field to a struct array

조회 수: 1,448(최근 30일)
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 = [];

  댓글 수: 0

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

채택된 답변

Walter Roberson
Walter Roberson 27 Jan 2018
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.

  댓글 수: 0

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

추가 답변(1개)

Kallam Haranadha Reddy
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';

  댓글 수: 0

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

Community Treasure Hunt

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

Start Hunting!

Translated by