merge structure field cells in matlab -
i want merge structure fields in case partial computations, later fill whole structure field cells.
the results put cells according index cell, this:
for i=3:4; results1.index{i}=i; results1.sqr{i}=i*i; end i=1; results2.index{i}=i; results2.sqr{i}=i*i; end
giving, respectively:
results1 = index: {[] [] [3] [4]} sqr: {[] [] [9] [16]} results2 = index: {[1]} sqr: {[1]}
is there way merge resulting structures obtain
allresults.index={[1] [] [3] [4]} allresults.sqr={[1] [] [9] [16]}
i can avoid overlapping results, no conflict resolution or overwriting in case of conflicting values (e.g. none of cells empty) ok. please note in larger dataset, cells not limited scalars may contain cells or other types.
you can first write small helper function merge cells:
function r = mergecells(a,b) r = {}; ia = ~cellfun('isempty',a); ib = ~cellfun('isempty',b); r(ia) = a(ia); r(ib) = b(ib); end
and call in loop merge fields
for k = 1:numel(f), f = f{k}; allresults.(f) = mergecells(results1.(f), results2.(f)); end
Comments
Post a Comment