-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetUserVars.m
58 lines (54 loc) · 2.54 KB
/
SetUserVars.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
58
function [EvalStr] = SetUserVars(VarList,varargin)
%SetUserVars Checks and sets user-definable variables
%
% [evalstr] = SetUserVars(VarList,UserVarCell)
%
% This function reads in a series of 'ArgName' -- 'ArgVal' parameters from
% the cell array 'UserVarCell' and checks them with the list of
% user-definable parameters in the cell array 'VarList'. It returns a
% string 'evalstr' that can be evaluated back in the calling function and
% that sets the parameters to either the user-defined values 'ArgVal' or
% the default values in 'VarList'. Use as in example below.
%
% function [] = DoIt(DoIt_in,varargin);
% %DoIt Executes some code
% %[DoIt_out] = DoIt(DoIt_in,'ArgName1',ArgVal1,'ArgName2',ArgVal2,...)
% %This function accepts a series of argument pairs to set user variables
%
% %User-definable parameters with default values:
% VarList = {'DoItParam1' 5.7; % numerical parameter
% 'DoItParam2' [1 0; 0 1]; % matrix parameter
% 'DoItParam3' 'none'}; % string parameter
% eval(SetUserVars(VarList,varargin)); % set the function parameters to either the
% % default values or user-defined values
%
% ... your "DoIt" function code using variables DoItParam1, DoItParam2, etc. ...
%
if length(varargin)==1,
varargin = varargin{1}; % if SetUserVars is called within other function as in the example above
end
if mod(length(varargin),2)~=0,
error('Number of arguments is odd: arguments must be passed in pairs (''ArgName'',ArgVal).');
end
EvalStr = [];
NumSetVars = 0;
for ii=1:size(VarList,1),
tmp = find(strcmp(varargin,VarList{ii,1})); % Find argument index in varargin cell vector.
if length(tmp)==1, % If argument is found and unique, set variable appropriately.
if ischar(varargin{tmp+1}),
EvalStr = [EvalStr VarList{ii,1} '=''' varargin{tmp+1} ''';'];
else
EvalStr = [EvalStr VarList{ii,1} '=' mat2str(varargin{tmp+1}) ';'];
end
NumSetVars = NumSetVars + 1; % Check the number of variables set from varargin.
else % Assign default value if parameter not passed as function argument.
if ischar(VarList{ii,2}),
EvalStr = [EvalStr VarList{ii,1} '=''' VarList{ii,2} ''';'];
else
EvalStr = [EvalStr VarList{ii,1} '=' mat2str(VarList{ii,2}) ';'];
end
end
end
if NumSetVars~=length(varargin)/2,
error('Some of the input arguments could not be set properly (check syntax)')
end