]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/options.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / options.m
1 ## Copyright (C) 2011   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn{Function File} {@var{opt} =} options (@var{"key1"}, @var{value1}, @var{"key2"}, @var{value2}, @dots{})
20 ## Create options struct @var{opt} from a number of key and value pairs.
21 ## For use with order reduction commands.
22 ##
23 ## @strong{Inputs}
24 ## @table @var
25 ## @item key, property
26 ## The name of the property.
27 ## @item value
28 ## The value of the property.
29 ## @end table
30 ##
31 ## @strong{Outputs}
32 ## @table @var
33 ## @item opt
34 ## Struct with fields for each key.
35 ## @end table
36 ##
37 ## @strong{Example}
38 ## @example
39 ## @group
40 ## octave:1> opt = options ("method", "spa", "tol", 1e-6)
41 ## opt =
42 ## 
43 ##   scalar structure containing the fields:
44 ## 
45 ##     method = spa
46 ##     tol =  1.0000e-06
47 ## 
48 ## @end group
49 ## @end example
50 ## @example
51 ## @group
52 ## octave:2> save filename opt
53 ## octave:3> # save the struct 'opt' to file 'filename' for later use
54 ## octave:4> load filename
55 ## octave:5> # load struct 'opt' from file 'filename'
56 ## @end group
57 ## @end example
58 ##
59 ## @end deftypefn
60
61 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
62 ## Created: November 2011
63 ## Version: 0.1
64
65 function opt = options (varargin)
66
67   if (nargin == 0)
68     print_usage ();
69   endif
70
71   if (rem (nargin, 2))
72     error ("options: properties and values must come in pairs");
73   endif
74
75   ## alternative: opt = struct (varargin{:});
76   
77   key = reshape (varargin(1:2:end-1), [], 1);
78   val = reshape (varargin(2:2:end), [], 1);
79
80   opt = cell2struct (val, key, 1);
81   opt = orderfields (opt);
82
83 endfunction