1 %# Copyright (C) 2006-2012, Thomas Treichl <treichl@users.sourceforge.net>
2 %# OdePkg - A package for solving ordinary differential equations and more
4 %# This program is free software; you can redistribute it and/or modify
5 %# it under the terms of the GNU General Public License as published by
6 %# the Free Software Foundation; either version 2 of the License, or
7 %# (at your option) any later version.
9 %# This program is distributed in the hope that it will be useful,
10 %# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %# GNU General Public License for more details.
14 %# You should have received a copy of the GNU General Public License
15 %# along with this program; If not, see <http://www.gnu.org/licenses/>.
18 %# @deftypefn {Function File} {[@var{value}] =} odeget (@var{odestruct}, @var{option}, [@var{default}])
19 %# @deftypefnx {Command} {[@var{values}] =} odeget (@var{odestruct}, @{@var{opt1}, @var{opt2}, @dots{}@}, [@{@var{def1}, @var{def2}, @dots{}@}])
21 %# If this function is called with two input arguments and the first input argument @var{odestruct} is of type structure array and the second input argument @var{option} is of type string then return the option value @var{value} that is specified by the option name @var{option} in the OdePkg option structure @var{odestruct}. Optionally if this function is called with a third input argument then return the default value @var{default} if @var{option} is not set in the structure @var{odestruct}.
23 %# If this function is called with two input arguments and the first input argument @var{odestruct} is of type structure array and the second input argument @var{option} is of type cell array of strings then return the option values @var{values} that are specified by the option names @var{opt1}, @var{opt2}, @dots{} in the OdePkg option structure @var{odestruct}. Optionally if this function is called with a third input argument of type cell array then return the default value @var{def1} if @var{opt1} is not set in the structure @var{odestruct}, @var{def2} if @var{opt2} is not set in the structure @var{odestruct}, @dots{}
25 %# Run examples with the command
33 %# Note: 20061022, Thomas Treichl
34 %# We cannot create a function of the form odeget (@var{odestruct},
35 %# @var{name1}, @var{name2}) because we would get a mismatch with
36 %# the function form 1 like described above.
38 function [vret] = odeget (varargin)
40 if (nargin == 0) %# Check number and types of input arguments
41 vmsg = sprintf ('Number of input arguments must be greater than zero');
42 help ('odeget'); error (vmsg);
43 elseif (isstruct (varargin{1}) == true) %# Check first input argument
44 vint.odestruct = odepkg_structure_check (varargin{1});
45 vint.otemplate = odeset; %# Create default odepkg otpions structure
47 vmsg = sprintf ('First input argument must be a valid odepkg otpions structure');
51 %# Check number and types of other input arguments
52 if (length (varargin) < 2 || length (varargin) > 3)
53 vmsg = sprintf ('odeget (odestruct, "option", default) or\n odeget (odestruct, {"opt1", "opt2", ...}, {def1, def2, ...})');
55 elseif (ischar (varargin{2}) == true && isempty (varargin{2}) == false)
56 vint.arguments = {varargin{2}};
58 elseif (iscellstr (varargin{2}) == true && isempty (varargin{2}) == false)
59 vint.arguments = varargin{2};
60 vint.lengtharg = length (vint.arguments);
63 if (nargin == 3) %# Check if third input argument is valid
64 if (iscell (varargin{3}) == true)
65 vint.defaults = varargin{3};
66 vint.lengthdf = length (vint.defaults);
68 vint.defaults = {varargin{3}};
71 if (vint.lengtharg ~= vint.lengthdf)
72 vmsg = sprintf ('If third argument is given then sizes of argument 2 and argument 3 must be the equal');
77 %# Run through number of input arguments given
78 for vcntarg = 1:vint.lengtharg
80 isempty(vint.odestruct.(vint.arguments{vcntarg}))
82 ischar(vint.odestruct.(vint.arguments{vcntarg})) && ...
83 strcmp(vint.odestruct.(vint.arguments{vcntarg}),vint.otemplate.(vint.arguments{vcntarg}))...
85 ~ischar(vint.odestruct.(vint.arguments{vcntarg})) && ...
86 vint.odestruct.(vint.arguments{vcntarg}) == vint.otemplate.(vint.arguments{vcntarg}) ...
88 if (nargin == 3), vint.returnval{vcntarg} = vint.defaults{vcntarg};
89 else, vint.returnval{vcntarg} = vint.odestruct.(vint.arguments{vcntarg}); end
90 else, vint.returnval{vcntarg} = vint.odestruct.(vint.arguments{vcntarg});
94 %# Postprocessing, store results in the vret variable
95 if (vint.lengtharg == 1), vret = vint.returnval{1};
96 else, vret = vint.returnval; end
98 %!test assert (odeget (odeset (), 'RelTol'), []);
99 %!test assert (odeget (odeset (), 'RelTol', 10), 10);
100 %!test assert (odeget (odeset (), {'RelTol', 'AbsTol'}), {[] []})
101 %!test assert (odeget (odeset (), {'RelTol', 'AbsTol'}, {10 20}), {10 20});
102 %!test assert (odeget (odeset (), 'Stats'), 'off');
103 %!test assert (odeget (odeset (), 'Stats', 'on'), 'on');
106 %! # Return the manually changed value RelTol of the OdePkg options
107 %! # strutcure A. If RelTol wouldn't have been changed then an
108 %! # empty matrix value would have been returned.
110 %! A = odeset ('RelTol', 1e-1, 'AbsTol', 1e-2);
111 %! odeget (A, 'RelTol', [])
114 %! # Return the manually changed value of RelTol and the value 1e-4
115 %! # for AbsTol of the OdePkg options structure A.
117 %! A = odeset ('RelTol', 1e-1);
118 %! odeget (A, {'RelTol', 'AbsTol'}, {1e-2, 1e-4})
120 %# Local Variables: ***