MATLAB: How to convert a structure array into vector

structure array to vector

Hello.
I have a structure array (1 x 50000) with 10 fields. The elements in the fields are arrays (1 x 50). I want to convert this structure array into a scalar structure with same fields such that, its size is (50000*50 x 1).

Best Answer

S(1).F1 = [1,2,3,4,5];
S(2).F1 = [10,20];
S(1).F2 = [2,4,6,8,10];
S(2).F2 = [150,200];
F = fieldnames(S);
C = num2cell(struct2cell(S),3);
C = cellfun(@(c)[c{:}],C,'uni',0);
C = num2cell(vertcat(C{:}));
T = cell2struct(C,F,1)
Giving:
T =
7x1 struct array with fields:
F1
F2
Checking the first field's values:
>> S.F1 % input
ans =
1 2 3 4 5
ans =
10 20
>> T.F1 % output
ans = 1
ans = 2
ans = 3
ans = 4
ans = 5
ans = 10
ans = 20