changeset 27426:3ec072ab1bda

namedargs2cell.m: New function (bug #56903). * scripts/miscellaneous/namedargs2cell.m: New function. * scripts/miscellaneous/module.mk: Add function to build system. * NEWS: Announce new function. * ov-cell.cc (Fstruct2cell): Add @seealso reference to namedargs2cell. * container.txi: Add DOCSTRING entry to manual.
author Rik <rik@octave.org>
date Tue, 17 Sep 2019 21:40:17 -0700
parents 633f7a8347c8
children cc7ab4ce0194
files NEWS doc/interpreter/container.txi libinterp/octave-value/ov-cell.cc scripts/miscellaneous/module.mk scripts/miscellaneous/namedargs2cell.m
diffstat 5 files changed, 69 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Tue Sep 17 20:38:02 2019 -0700
+++ b/NEWS	Tue Sep 17 21:40:17 2019 -0700
@@ -87,6 +87,7 @@
 ### Alphabetical list of new functions added in Octave 6
 
 - `lightangle`
+- `namedargs2cell`
 - `newline`
 - `rotx`
 - `roty`
--- a/doc/interpreter/container.txi	Tue Sep 17 20:38:02 2019 -0700
+++ b/doc/interpreter/container.txi	Tue Sep 17 21:40:17 2019 -0700
@@ -550,6 +550,8 @@
 
 @DOCSTRING(struct2cell)
 
+@DOCSTRING(namedargs2cell)
+
 @node containers.Map
 @section containers.Map
 @cindex Map
--- a/libinterp/octave-value/ov-cell.cc	Tue Sep 17 20:38:02 2019 -0700
+++ b/libinterp/octave-value/ov-cell.cc	Tue Sep 17 21:40:17 2019 -0700
@@ -1355,7 +1355,7 @@
 @end group
 @end example
 
-@seealso{cell2struct, fieldnames}
+@seealso{cell2struct, namedargs2cell, fieldnames}
 @end deftypefn */)
 {
   if (args.length () != 1)
--- a/scripts/miscellaneous/module.mk	Tue Sep 17 20:38:02 2019 -0700
+++ b/scripts/miscellaneous/module.mk	Tue Sep 17 21:40:17 2019 -0700
@@ -52,6 +52,7 @@
   %reldir%/mkdir.m \
   %reldir%/mkoctfile.m \
   %reldir%/movefile.m \
+  %reldir%/namedargs2cell.m \
   %reldir%/namelengthmax.m \
   %reldir%/nargchk.m \
   %reldir%/narginchk.m \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/namedargs2cell.m	Tue Sep 17 21:40:17 2019 -0700
@@ -0,0 +1,64 @@
+## Copyright (C) 2019 Guillaume Flandin
+##
+## This file is part of Octave.
+##
+## Octave is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <https://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {} {@var{c} =} namedargs2cell (@var{s})
+## Create a cell array of field name/value pairs from a scalar structure.
+##
+## Example:
+##
+## @example
+## @group
+## @c doctest: +SKIP
+## s.Name = "Peter";
+## s.Height = 185;
+## s.Age = 42;
+##
+## c = namedargs2cell (s)
+##   @result{} @{ "Name", "Peter", "Height", 185, "Age", 42 @}
+## @end group
+## @end example
+##
+## @seealso{struct2cell}
+## @end deftypefn
+
+function c = namedargs2cell (s)
+  
+  if (nargin != 1 || nargout > 1)
+    print_usage ();
+  endif
+  if (! isstruct (s) || ! isscalar (s))
+    error ("namedargs2cell: S must be a scalar structure");
+  endif
+  
+  c = reshape ([fieldnames(s), struct2cell(s)].', 1, []);
+
+endfunction
+
+
+%!test
+%! data = { "Name", "Peter", "Height", 185, "Age", 42};
+%! s = struct (data{:});
+%! c = namedargs2cell (s);
+%! assert (isequal (c, data));
+
+## Test input validation
+%!error <Invalid call> namedargs2cell ()
+%!error <Invalid call> namedargs2cell (1, 2)
+%!error <S must be a scalar structure> namedargs2cell (true)
+%!error <S must be a scalar structure> namedargs2cell (struct ("name", {1, 2}))