-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFields.m
30 lines (30 loc) · 1.05 KB
/
getFields.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
%% getFields: Get fields matching input from struct.
%
% inStruct: Struct from which fields is to be extracted.
% inString: String with the field text. A '$' is used as
% a placeholder for the elements in 'specifier'.
% specifier: Vector with values to replace the '$' in 'inString'.
%
% RETURNS: Cell-array or Matrix (if all output is numeric).
%
function [out] = getFields(inStruct, inString, specifier)
out = cell(1, length(specifier));
isDouble = true;
for ii = 1:length(specifier)
field = strrep( strtrim(inString), '$', num2str(specifier(ii)) );
try
out{ii} = inStruct.(field);
catch
try
out{ii} = inStruct.(['MARS_' field]);
warning(['No field named, ' field ', trying to prepend string ''MARS_''!'])
catch
error(['No field in struct named ''' field ''' or ''MARS_' field '.']);
end
isDouble = isDouble && isa(out{ii}, 'double');
end
end
if isDouble
out = cell2mat(out);
end
end