-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct2array.m
38 lines (31 loc) · 918 Bytes
/
struct2array.m
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
% STRUCT2ARRAY.M
% function ArrayOut = struct2array(structIn,fieldname);
% For vector or scalar fields only. 'ArrayOut' is of size
% #structure elements x size of field.
%
function ArrayOut = struct2array(structIn,fieldname);
nIn = length(structIn);
if nargin < 2
disp(structIn);
fieldname = input('Which field name? ','s');
end;
status = 1; i = 1;
while status % first, check validity of the field
temp = getfield(structIn(i),fieldname);
if ~isempty(temp) status = 0; end;
i = i + 1;
if i > nIn status = 0; end;
end;
lgth = length(temp);
if size(temp,1)*size(temp,2) ~= lgth
error('All fields must contain vector or scalar values only.');
end;
ArrayOut = zeros(nIn,lgth); % then actually get the field's values
for i = 1:nIn
temp = getfield(structIn(i),fieldname);
if ~isempty(temp)
ArrayOut(i,:) = temp;
else
ArrayOut(i,:) = inf * ones(1,lgth);
end;
end;