1+ function pStruct = checkSanityOfStructs(pStruct , type )
2+ % checkSanityOfStructs.m checks if the necessary fields of the parameters
3+ % or the properties struct are set. If necessary fields are missing but can
4+ % unambiguously, this will be done. Otherwise, an error will be displayed.
5+ %
6+ % USAGE:
7+ % pStruct = checkSanityOfStructs(pStruct, type)
8+ %
9+ % Parameters:
10+ % pStruct: parameters or properties struct.
11+ % type: string, either 'parameters' or 'properties'
12+ %
13+ % Return values:
14+ % pSturct: struct containing informations about the parameters or the
15+ % properties
16+ %
17+ % History:
18+ % * 2017/05/21 Paul Stapor
19+
20+
21+
22+ % See which struct should be checked
23+ if ~(strcmp(type , ' parameters' ) || strcmp(type , ' properties' ))
24+ error(' The given type of struct is invalid. Only parameters and properties and be checked by this function.' );
25+ else
26+ if strcmp(type , ' parameters' )
27+ short_type = ' par' ;
28+ else
29+ short_type = ' prop' ;
30+ end
31+ end
32+
33+ % The least necessary information are lower and upper bounds for parameters
34+ if ~isfield(pStruct , ' min' )
35+ error([' The struct ' type ' has no lower bounds provided.' ]);
36+ end
37+ if ~isfield(pStruct , ' max' )
38+ error([' The struct ' type ' has no upper bounds provided.' ]);
39+ end
40+
41+ % Upper and lower bounds need to have the same length
42+ if ~all(size(pStruct .min ) == size(pStruct .max ))
43+ error([' The vectors for the lower and upper bounds of' type ' do not have the same size.' ]);
44+ end
45+
46+ % The number of pStruct needs to be set accordingly
47+ if ~isfield(pStruct , ' number' )
48+ pStruct.number = length(pStruct .min );
49+ else
50+ if (length(pStruct .min ) ~= pStruct .number )
51+ warning([' The size of struct ' type ' was set incorrectly in ' type ' .number. This was corrected.' ]);
52+ end
53+ end
54+
55+ % The names of the parameters have to be set correctly
56+ if ~isfield(pStruct , ' name' )
57+ pStruct.name = arrayfun(@(x ) [short_type num2str(x )], 1 : pStruct .number , ' UniformOutput' , false );
58+ else
59+ if (length(pStruct .name ) ~= pStruct .number )
60+ error([' The names for the struct ' type ' did not have the correct length. Either provide them with correct length or leave ' type ' .name empty.' ]);
61+ end
62+ end
63+
64+ end
0 commit comments