function A = struct_of_arrays_to_array_of_structs(S)

fs = fieldnames(S);
for i=1:length(fs)
    if i==1
        N = size(S.(fs{i}),1);
        A = repmat(struct(),N,1);
    else
        assert(N==size(S.(fs{i}),1));
    end
    
    
    if iscell(S.(fs{i}))
        for j=1:N
            A(j).(fs{i}) = S.(fs{i}){j,:};
        end
    else
        for j=1:N
            A(j).(fs{i}) = S.(fs{i})(j,:);
        end
    end    
end


end