File: GroupStructArrayByFields.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (33 lines) | stat: -rw-r--r-- 999 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function theGroupedArray = GroupStructArrayByFields(theStructArray,theFields)
% theGroupedArray = GroupStructArrayByFields(theStructArray,theFields)
%
% Group together the members of a struct array that share the same values
% in the passed fields.
%
% This is useful for sorting/grouping elements in a struct array on
% parameters that signify membership of trials to a particular condition.
%
% 7/21/03  dhb  Wrote it.

theGroupedArray = {};
nStructs = length(theStructArray);

% The first passed structure is equal to itself
nGroups = 1;
theGroupedArray{1} = theStructArray(1);

% Put structures into groups, creating new ones as necessary.
for i = 2:nStructs
	didIt = 0;
	for j = 1:nGroups
		if AreStructsEqualOnFields(theStructArray(i),theGroupedArray{j}(1),theFields)
			theGroupedArray{j} = [theGroupedArray{j} theStructArray(i)];
			didIt = 1;
			break;
		end
	end
	if (~didIt)
		nGroups = nGroups+1;
		theGroupedArray{nGroups} = theStructArray(i);
	end
end