]> Creatis software - CreaPhase.git/blob - octave_packages/struct-1.0.10/getfields.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / struct-1.0.10 / getfields.m
1 ## Copyright (C) 2000 Etienne Grossmann <etienne@egdn.net>
2 ## Copyright (C) 2012 Olaf Till <i7tiol@t-online.de>
3 ##
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
7 ## version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Built-in Function} {} [@var{v1},...] =
19 ## getfields (@var{s}, 'k1',...) = [@var{s}.k1,...]
20 ## Return selected values from a scalar struct. Provides some
21 ## compatibility and some flexibility.
22 ## @end deftypefn
23 ## @seealso{setfields,rmfield,isfield,isstruct,struct}
24
25 function [varargout] = getfields (s, varargin)
26
27   if (! all (isfield (s, varargin)))
28     error ("some fields not present");
29   endif
30
31   if (all (size (s) <= 1))
32     varargout = fields2cell (s, varargin);
33   else
34     error ("structure must be scalar or empty");
35   endif
36
37 endfunction
38
39 %!assert (getfields (struct ('key', 'value'), 'key'), 'value');