from
Append a new entry to structure array, fields don't need to be same
by Misha Koshelev
Appends a new entry to structure array, fields don't need to be same. Can also insert a new entry.
|
| append_struct_array(struct, field, elem, idx_elem) |
function new_struct = append_struct_array(struct, field, elem, idx_elem)
% APPEND_STRUCT_ARRAY Append an element to a structure array in a (parent) structure.
% NEW_STRUCT = APPEND_STRUCT_ARRAY(STRUCT, FIELD, ELEM, IDX_ELEM) Append
% element ELEM to the structure array named FIELD in parent structure
% STRUCT. Returns a new parent structure NEW_STRUCT. If this field does
% not exist, create new one.
%
% Optional IDX_ELEM parameter causes the specified element number to be replaced.
%
% Misha Koshelev
% July 29th, 2009
% Montague Laboratory
if ~isfield(struct, field) || ...
isempty(struct.(field))
struct.(field) = elem;
else
if ~exist('idx_elem', 'var')
% New element
idx_elem = length(struct.(field))+1;
end
% Field names in new element
elem_field = fieldnames(elem);
for idx_elem_field=1:length(elem_field)
elem_field_this = elem_field{idx_elem_field};
struct.(field)(idx_elem).(elem_field_this) = ...
elem.(elem_field_this);
end
end
new_struct = struct;
|
|
Contact us at files@mathworks.com