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

 Akzeptierte Antwort

Stephen23
Stephen23 am 16 Okt. 2018
Bearbeitet: Stephen23 am 16 Okt. 2018

0 Stimmen

Here is a 1x3 structure with two 1x4 numeric fields:
S(1).f1 = 0:3;
S(1).f2 = 2:5;
S(2).f1 = 1:4;
S(2).f2 = 3:6;
S(3).f1 = 4:7;
S(3).f2 = 5:8;
Convert this to a 1x4 structure with two 1x3 numeric fields:
C = num2cell(cell2mat(struct2cell(S)),3);
C = cellfun(@(v)v(:).',C,'uni',0);
T = cell2struct(C,fieldnames(S),1).';
Checking the sizes:
>> size(S)
ans =
1 3
>> size(T)
ans =
1 4
And checking the field data:
>> T.f1
ans =
0 1 4
ans =
1 2 5
ans =
2 3 6
ans =
3 4 7
>> T.f2
ans =
2 3 5
ans =
3 4 6
ans =
4 5 7
ans =
5 6 8

3 Kommentare

Guillaume
Guillaume am 16 Okt. 2018
Note that this solution has the same requirements as mine: struct and fields must be row vectors (otherwise cell2mat starts concatenating fields and fields content).
The only difference between the two solutions is how to go from the matrix to the reordered cell array. I use permute, Stephen uses a cellfun with a reshape and transpose.
Ted Miller
Ted Miller am 16 Okt. 2018
Stephen, Guillaume, Thanks for the fast code and fast response. Apparently, cell2mat works if all fields are doubles. What to do if fld1 is double and fld2 is int32?
Stephen23
Stephen23 am 16 Okt. 2018
Bearbeitet: Stephen23 am 16 Okt. 2018
"What to do if fld1 is double and fld2 is int32?"
Then you will have to split the data into a nested cell array before concatenating it:
C1 = num2cell(cat(1,S.f1).',2);
C2 = num2cell(cat(1,S.f2).',2);
T = cell2struct([C1,C2],fieldnames(S),2)
And checking:
>> size(T)
ans =
4 1
>> T.f1
ans =
0 1 4
ans =
1 2 5
ans =
2 3 6
ans =
3 4 7
>> T.f2
ans =
2 3 5
ans =
3 4 6
ans =
4 5 7
ans =
5 6 8

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 16 Okt. 2018

0 Stimmen

Convert to cell array with struct2cell, then to matrix, rearrange the dimension, and convert back to cell array then structure:
If the fields and the structure are row vectors:
tempmat = cell2mat(struct2cell(x)); %note that the cell2mat call will fail if not all fields are the same size
tempmat = permute(tempmat, [1 3 2]);
newx = cell2struct(num2cell(tempmat, 2), fieldnames(x), 1)
If the field or the structure or both are not row vectors, then specify what they are as writing a generic version that can cope with vectors in any dimension would be a pain.

Kategorien

Gefragt:

am 16 Okt. 2018

Bearbeitet:

am 16 Okt. 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by