I have a struct array, x, with two fields, x.fld1 and x.fld2. x has n indices and all fields have m indices like x(1:n).fld1(1:m) and x(1:n).fld2(1:m). I want to re-order the struct to y(1:m).fld1(1:n) and y(1:m).fld2(1:n). Any ideas besides a triple for-loop cycling over the x indices, the fields, and the fld indices? Also, how would you pre-allocate y?
% my long way flds = fieldnames(x); nflds = length( flds ); n = length(x); m = length(x(1).fld{1}); for i = 1:n for j = 1:m for k = 1:nflds y(i).(flds{k})(j) = x(j).(flds{k})(i); end end end
Best Answer