structures consolidation and manipulations
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello I have a basic question on structures I have a struct a which can contain n fields So for example
a(1)
=
mydata:[135x6]
myid:{1x6}
mydates = [135x1]
mytel=
myname=
etc
and so on for a(2), a(3)...etc to m
I want to then do some manipulation on the data say my own function transform which takes a(1) as an input and produces a(1).stats
Then I want to consolidate all my m objects into one big structure say BigStruct that has all the fields that a had plus the stats
such that
BigStruct =
mydata =[135x1000]
mydates = [135x1000]
etc
etc
Again I am doing all this in a for loop and was wondering if there is an easier elegant method to do this
Thanks so much
댓글 수: 2
Azzi Abdelmalek
2013년 8월 21일
편집: Azzi Abdelmalek
2013년 8월 21일
You did not specify if the concatenation is always horizontal or vertical?
채택된 답변
Walter Roberson
2013년 8월 21일
Tstats = arrayfun( @(K) yourStats(a(K)), 1:length(a), 'Uniform', 0);
T1 = struct2cell(a(:));
T2 = cat(2, T1);
T3 = horzcat(T2, Tstats);
Now use cell2struct() or the like to convert 32 back into a cell with appropriate field names e.g. [fieldnames(a); {'stats'}]
Watch out though for the dimension you concatenate on for mydata: somehow you went from 1000 of (135 x 6) to a single 135 x 1000 leaving us uncertain what happened to the x 6.
추가 답변 (1개)
Azzi Abdelmalek
2013년 8월 21일
편집: Azzi Abdelmalek
2013년 8월 21일
For vertical concatenation
names=fieldnames(a);
for k=1:numel(names)
b.(names{k})=cat(1,a(:).(names{k}));
end
b
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!