File: Struct2Vect.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 (48 lines) | stat: -rw-r--r-- 1,577 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function vec = Struct2Vect(struc,fieldnm)
% vec = Struct2Vect(struc,fieldnm)
% Traverses array of structs STRUC and returns data from all fields FIELDNM
% in vector VEC.
% Returns array vector if field contains numeric scalars or scalar structs,
% cell vector otherwise. If concatenating struct arrays, each struct must
% contain the same fields
%
% Example: for field data.field:
%   data(1).field = 23;
%   data(2).field = 56;
%   vec = Struct2Vect(data,'field')
%   vec = [23 56];
% 
% Example: for field data2.test:
%   data2(1).test = 23;
%   data2(2).test = 'd';
%   vec = Struct2Vect(data2,'test')
%   vec = {23 'd};
% 
% Example: for field data3.cells:
%   data3(1).cells = 23;
%   data3(2).cells = [42 45];
%   vec = Struct2Vect(data3,'cells')
%   vec = {23, [42 45]};
% 
% Example: for field data4.struc:
%   data4(1).struc.a = 24;
%   data4(2).struc.a = [42 45];
%   vec = Struct2Vect(data4,'struc')
%   vec(1).a = 24;
%   vec(2).a = [42 45];


% DN 2007
% DN 2008-07-30 Fixed handling of numeric vectors
% DN 2012-06-12 Better tests for numeric scalar, now tests all elements in
%               array. Can now handle struct arrays

isnumer = arrayfun(@(x) isnumeric(x.(fieldnm)),struc); isnumer = all(isnumer(:));
isstruc = arrayfun(@(x) isstruct(x.(fieldnm)),struc);  isstruc = all(isstruc(:));
isscala = arrayfun(@(x) isscalar(x.(fieldnm)),struc);  isscala = all(isscala(:));

if (isnumer || isstruc) && isscala
    vec = [struc.(fieldnm)];
else % not numeric scalar, make cellarray
    vec = {struc.(fieldnm)};
end