Hello, I will use a simple example to show what I am after. I have a structure array 'person' with fields ('First_Name' , 'Last_Name' , 'FamilyInfo'), where 'FamilyInfo' is a structure with three fields ('FatherName' , 'MotherName' , 'NumberOfSiblings').
I create 4 elements in person structure array and fill all the 'First_Name' and 'Last_Name' fields but the 'FamilyInfo' field is left empty. Now, for all the 4 elements in the 'person' structure array, I want to assign at once the same value to the field "NumberOfsiblings" in the nested structure 'Family', but MATLAB throws an error. Here is the code;
Create an empty structre array
person = struct('First_Name',[],'Last_Name',[],...'FamilyInfo',struct('FatherName',[],'MotherName',[],'NumberOfsiblings',[]));
Create elements in the array but the Family field is left empty
FirstNames = {'Joe','Jack','William','Averel'};[person(1:4).First_Name]=deal(FirstNames{:});[person(1:4).Last_Name]=deal('Dalton');
For all the elements in person structure array, assign the same value (3) to the NumberOfsiblings field, of the Family field.
[person(1:4).FamilyInfo.NumerOfsiblings] = 3;
Error message pops up;
Expected one output from a curly brace or dot indexing expression, but there were 4 results.
Is there a way to do it without using a for loop? One-line code is preferred. Thanks
Best Answer