'How can I reorganize a structure that's organized by time in MATLAB?

suppose I have data of various items (x) at discrete time events (t1 ... tn), organized in a structure S. The fields of the structure are of the form:

S.t1.x = x(t1) S.t2.x = x(t2) ... S.tn.x = x(tn), etc.

where each subfield has only one value from the whole x

How best could I rearrange this into a structure of the form

S.x = x(t1 ... tn)?

For now I am looping through each time field and assigning the corresponding variable to the appropriate time index in a different structure, as in

for i = 1:length(t)

X(i) = S.(t(i)).x;
Y(i) = S.(t(i)).y;

end

with some vectorization so I don't actually have to assign each individual variable.

This gets the results I need, but since there is over a gigabyte of data to convert, it takes a while and I'd like to make this as efficient as possible. Is there an algorithm for doing this any better?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source