]> Creatis software - CreaPhase.git/blob - octave_packages/odepkg-0.8.2/odeget.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / odepkg-0.8.2 / odeget.m
1 %# Copyright (C) 2006-2012, Thomas Treichl <treichl@users.sourceforge.net>
2 %# OdePkg - A package for solving ordinary differential equations and more
3 %#
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.
8 %#
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.
13 %#
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/>.
16
17 %# -*- texinfo -*-
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{}@}])
20 %#
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}.
22 %#
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{}
24 %#
25 %# Run examples with the command
26 %# @example
27 %# demo odeget
28 %# @end example
29 %# @end deftypefn
30 %#
31 %# @seealso{odepkg}
32
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.
37
38 function [vret] = odeget (varargin)
39
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
46   else
47     vmsg = sprintf ('First input argument must be a valid odepkg otpions structure');
48     error (vmsg);
49   end
50
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, ...})');
54     usage (vmsg);
55   elseif (ischar (varargin{2}) == true && isempty (varargin{2}) == false)
56     vint.arguments = {varargin{2}};
57     vint.lengtharg = 1;
58   elseif (iscellstr (varargin{2}) == true && isempty (varargin{2}) == false)
59     vint.arguments = varargin{2};
60     vint.lengtharg = length (vint.arguments);
61   end
62
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);
67     else
68       vint.defaults = {varargin{3}};
69       vint.lengthdf = 1;
70     end
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');
73       error (vmsg);
74     end
75   end
76
77   %# Run through number of input arguments given
78   for vcntarg = 1:vint.lengtharg
79     if ((...
80          isempty(vint.odestruct.(vint.arguments{vcntarg}))
81         )||( ...
82          ischar(vint.odestruct.(vint.arguments{vcntarg})) && ...
83          strcmp(vint.odestruct.(vint.arguments{vcntarg}),vint.otemplate.(vint.arguments{vcntarg}))...
84         )||(...
85          ~ischar(vint.odestruct.(vint.arguments{vcntarg})) && ...
86          vint.odestruct.(vint.arguments{vcntarg}) == vint.otemplate.(vint.arguments{vcntarg}) ...
87         ))
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});
91     end
92   end
93
94   %# Postprocessing, store results in the vret variable
95   if (vint.lengtharg == 1), vret = vint.returnval{1};
96   else, vret = vint.returnval; end
97
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');
104
105 %!demo
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.
109 %!
110 %! A = odeset ('RelTol', 1e-1, 'AbsTol', 1e-2);
111 %! odeget (A, 'RelTol', [])
112
113 %!demo
114 %! # Return the manually changed value of RelTol and the value 1e-4
115 %! # for AbsTol of the OdePkg options structure A.
116 %!
117 %! A = odeset ('RelTol', 1e-1);
118 %! odeget (A, {'RelTol', 'AbsTol'}, {1e-2, 1e-4})
119
120 %# Local Variables: ***
121 %# mode: octave ***
122 %# End: ***