MATLAB: How to Expand an Existing Structure Array struct As a simple example, I create the struct B with one field: B.D = 1; How do I set parent struct A for the above-mentioned struct B? A.B.D How do I set a field between B and D? B.C.D Best Answer "How do I set parent struct A for the above-mentioned struct B?"A.B = B;"How do I set a field between B and D?"temp = B.D;B = rmfield(B, 'D');B.C.D = temp; Related SolutionsMATLAB: How to Access to Fields of a Non-scalar Struct D = arrayfun(@(IDX) A(IDX).B.C, (1:numel(A)).', 'Uniform', 0);This is the safest approach, as it will work for the case where there are fields in addition to B within A, where some other approaches would not.But in the specialized case where B is the only field inside A, thentemp = cell2mat(struct2cell(A));D = {temp.C}.'; MATLAB: How to access a field of a struct by indexing A_cell = struct2cell(A);D = A_cell{3}Keep in mind that the order of the fields of structs is not necessarily constant:A.B = 1;A.C = 2;A.D = 3;B.B = 1;B.D = 3;B.C = 2;isequal(A, B) % >> TRUE! Related QuestionHow to delete a field from a structAdding dissimilar structures together (not overwriting similar field values)How to make a structure to be input of a function and then its updated version to be output of the functionStructures and cells – how to modify and saveDelete fields of struct based on starting charactersAssign a ‘double’ variable to struct
Best Answer