xxsimSetSettings - Change the value of one or more 20-sim tool or model settings. Syntax: result = xxsimSetSettings(keys, values) Inputs: keys = a list of keys to apply. values = a list of accompanying values to apply. -or- key_value = a list of key-value structs. Outputs: result = returns true if the setting has been set successfully, false otherwise. Examples: result = xxsimSetSettings('model.simulator.integrationmethod', 'Euler'); result = xxsimSetSettings({'model.simulator.integrationmethod'}, {'Euler'}); result = xxsimSetSettings({'model.simulator.integrationmethod', 'model.simulator.integrationmethod.Euler.stepsize'}, {'Euler', '0.01'}); result = xxsimSetSettings([struct('key', 'model.simulator.starttime', 'value', '0.0'), struct('key', 'model.simulator.finishtime', 'value', '20.0')]); See also: xxsimQuerySettings Author: Controllab Products B.V. email: info@controllab.nl Website: http://www.controllab.nl October 2014
0001 function result = xxsimSetSettings(varargin) 0002 % xxsimSetSettings - Change the value of one or more 20-sim tool or model settings. 0003 % 0004 % Syntax: 0005 % result = xxsimSetSettings(keys, values) 0006 % 0007 % Inputs: 0008 % keys = a list of keys to apply. 0009 % values = a list of accompanying values to apply. 0010 % -or- 0011 % key_value = a list of key-value structs. 0012 % 0013 % Outputs: 0014 % result = returns true if the setting has been set successfully, false otherwise. 0015 % 0016 % Examples: 0017 % result = xxsimSetSettings('model.simulator.integrationmethod', 'Euler'); 0018 % result = xxsimSetSettings({'model.simulator.integrationmethod'}, {'Euler'}); 0019 % result = xxsimSetSettings({'model.simulator.integrationmethod', 'model.simulator.integrationmethod.Euler.stepsize'}, {'Euler', '0.01'}); 0020 % result = xxsimSetSettings([struct('key', 'model.simulator.starttime', 'value', '0.0'), struct('key', 'model.simulator.finishtime', 'value', '20.0')]); 0021 % 0022 % See also: xxsimQuerySettings 0023 % 0024 % Author: Controllab Products B.V. 0025 % email: info@controllab.nl 0026 % Website: http://www.controllab.nl 0027 % October 2014 0028 0029 %------------- BEGIN CODE -------------- 0030 global xxsim; 0031 xxsimVerbose = 0; 0032 if exist('xxsim') 0033 xxsimVerbose = xxsim.verbose; 0034 end 0035 0036 result = false; 0037 0038 0039 % fetch the input arguments 0040 if (nargin == 1) 0041 % array with key,value pairs 0042 key_value_array = varargin{1}; 0043 if isvector(key_value_array) 0044 % check if the first element is indeed a key-value struct 0045 elem_check = key_value_array(1); 0046 if ~isstruct(elem_check) 0047 error('Input argument mismatch. Expected an array of key,value structs'); 0048 return; 0049 else 0050 % ok, we have a struct, now check if this is a key,value struct 0051 if ~isfield(elem_check, 'key') 0052 error('Input argument mismatch. Struct does not contain a key field.'); 0053 return; 0054 end 0055 if ~isfield(elem_check, 'value') 0056 error('Input argument mismatch. Struct does not contain a value field.'); 0057 return; 0058 end 0059 end 0060 elseif (isstruct(key_value_array)) 0061 % ok, we have a struct, now check if this is a key,value struct 0062 if ~isfield(key_value_array, 'key') 0063 error('Input argument mismatch. Struct does not contain a key field.'); 0064 return; 0065 end 0066 if ~isfield(key_value_array, 'value') 0067 error('Input argument mismatch. Struct does not contain a value field.'); 0068 return; 0069 end 0070 else 0071 error('Input argument mismatch. Expected an array of key,value structs'); 0072 return; 0073 end 0074 settings = key_value_array; 0075 elseif (nargin == 2) 0076 % check input arguments 0077 use_keys = {}; 0078 use_values = {}; 0079 0080 keys = varargin{1}; 0081 values = varargin{2}; 0082 0083 if ~ischar(keys) 0084 use_keys = keys; 0085 else 0086 use_keys = {keys}; 0087 end 0088 0089 if length(use_keys) == 0 0090 error('no keys are given'); 0091 return; 0092 end 0093 0094 if iscell(values) 0095 use_values = values; 0096 else 0097 use_values = {values}; 0098 end 0099 0100 % test if the given arguments have the same length 0101 if length(use_keys) ~= length(use_values) 0102 error('array size mismatch in given keys and values'); 0103 return; 0104 end 0105 0106 % create a an array 'settings' of struct with name and values 0107 for i=1:length(use_keys) 0108 %For matlab compatibility create the first element without index. 0109 if i == 1 0110 settings = struct('key', use_keys{i}, 'value', use_values{i}); 0111 else 0112 settings(i)= struct('key', use_keys{i}, 'value', use_values{i}); 0113 end 0114 end 0115 else 0116 error('Wrong number of arguments. Function expects 1 or 2 arguments.'); 0117 return; 0118 end 0119 0120 0121 if xxsimVerbose then 0122 %print the settings 0123 settings 0124 end 0125 0126 % do the actual invoke 0127 result = xrlinvoke('xxsim.setSettings', struct('settings', {{ settings, 'array' }})); 0128 0129 end 0130 %------------- END CODE --------------