-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFieldFromStruct.m
executable file
·57 lines (51 loc) · 1.34 KB
/
getFieldFromStruct.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function fieldOut = getFieldFromStruct(S,fieldsString, matBool)
%% getFieldFromStruct
% Author: Erik Roberts
%
% Purpose: Get data from a field in a nested structure array
%
% Usage: fieldOut = getFieldFromStruct(Structure,fieldsString)
% fieldOut = getFieldFromStruct(Structure,fieldsString, matBool)
%
% INPUT (Required)
% Structure: must be structure scalar or vector (ie not matrix)
% fieldsString: string containing nested fields
%
% INPUT (Optional)
% matBool: logical of whether to try to convert cell array to mat (default:1)
%
% OUTPUT
% fieldOut: cell array of contents. returns [] if field not found.
%
% Example
% >> S.a.b.c.d = 1;
% >> fieldOut = getFieldFromStructArray(S,'a.b.c.d');
% fieldOut =
% {[1]};
nS = length(S);
fieldCells = textscan(fieldsString,'%s','Delimiter','.');
fieldCells = fieldCells{1};
for iS = 1:nS
fieldOut{iS} = recurvField(S, fieldCells);
end
matBool = setDefault('matBool', 1);
if matBool
try
fieldOut = cell2mat(fieldOut);
end
end
%% recursive sub function
function fieldOut = recurvField(S, fieldCells)
if ~isempty(fieldCells)
try
S = S.(fieldCells{1});
fieldCells(1) = [];
fieldOut = recurvField(S, fieldCells);
catch
fieldOut = []; %if not a valid subfield, return empty mat
end
else
fieldOut = S;
end
end
end