# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1490414625 14400
#      Sat Mar 25 00:03:45 2017 -0400
# Node ID 7ccdb9ba530b2f5b325bb2dce594e953312ed268
# Parent  e4bef84213e75fc259420235d61d06648f61d17d
Implemented missing function struct2array.m (bug #50622)

* /scripts/general/struct2array.m: new function m-file, primarily a wrapper for struct2cell, flattening the output with {:}
* /scripts/general/module.mk: added struct2array.m to scripts_general_FCN_FILES list
* /doc/interpreter/quad.txi: added doc reference to struct2array after struct2cell
* /NEWS: added struct2array under 'Other new functions added in 4.4'

diff -r e4bef84213e7 -r 7ccdb9ba530b NEWS
--- a/NEWS	Thu Mar 23 09:13:11 2017 +0100
+++ b/NEWS	Sat Mar 25 00:03:45 2017 -0400
@@ -23,6 +23,7 @@
       corrcoef
       gsvd
       hgtransform
+		struct2array
 
  ** Deprecated functions.
 
diff -r e4bef84213e7 -r 7ccdb9ba530b doc/interpreter/container.txi
--- a/doc/interpreter/container.txi	Thu Mar 23 09:13:11 2017 +0100
+++ b/doc/interpreter/container.txi	Sat Mar 25 00:03:45 2017 -0400
@@ -537,6 +537,8 @@
 
 @DOCSTRING(struct2cell)
 
+@DOCSTRING(struct2array)
+
 @node Cell Arrays
 @section Cell Arrays
 @cindex cell arrays
diff -r e4bef84213e7 -r 7ccdb9ba530b scripts/general/module.mk
--- a/scripts/general/module.mk	Thu Mar 23 09:13:11 2017 +0100
+++ b/scripts/general/module.mk	Sat Mar 25 00:03:45 2017 -0400
@@ -77,6 +77,7 @@
   scripts/general/shiftdim.m \
   scripts/general/sortrows.m \
   scripts/general/sph2cart.m \
+  scripts/general/struct2array.m \
   scripts/general/structfun.m \
   scripts/general/subsindex.m \
   scripts/general/trapz.m \
diff -r e4bef84213e7 -r 7ccdb9ba530b scripts/general/struct2array.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/general/struct2array.m	Sat Mar 25 00:03:45 2017 -0400
@@ -0,0 +1,90 @@
+## Copyright (C) 2017 Nicholas Jankowski
+## 
+## This program 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.
+## 
+## This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*- 
+## @deftypefn {} {@var{a} =} struct2array (@var{s})
+##
+## Convert a structure, @var{s}, into a numeric array, @var{a}
+##
+## @code{struct2array} will attempt to horizontally concatenate the contents of 
+## the input structure. It does this by first converting the structure to a cell
+## array, and then concatenating the resulting cells into an array. 
+##
+## Arrays in each field must have the same number of elements in each dimension 
+## except for dim2 (columns).
+##
+## @seealso{struct2cell, cell2struct, struct, fieldnames, }
+## @end deftypefn
+
+## Author: Nicholas Jankowski <jankowskin@asme.org>
+## Created: 2017-03-23
+
+function retval = struct2array (input_struct)
+  ##input check
+  if (~isstruct (input_struct) || (nargin ~= 1))
+    print_usage;
+  endif
+  
+  ##convert to cell array and flatten/concatenate output.
+  retval = [ (struct2cell (input_struct)){:}];
+endfunction
+
+%!test
+%! assert (struct2array (struct ('a', 1, 'b', 2)), [1, 2])
+%! assert (struct2array (struct ('a', [1; 2], 'b', [3; 4])), [1, 3; 2, 4])
+%! assert (struct2array (struct ('a', 'foo', 'b', 'bar')), 'foobar')
+%! assert (struct2array (struct ('a', [])), [])
+%! assert (struct2array (struct ('a', [], 'b', NaN)), NaN)
+
+%!test
+%! x.a = 1;
+%! x.b = {'Cell', 'Array'};
+%! x.c = 2;
+%! rightans = {[1], 'Cell', 'Array', [2]};
+%! assert (struct2array (x), rightans);
+
+%!test
+%! x(1,1).a = 1;
+%! x(1,2).a = 2;
+%! x(2,1).a = 3;
+%! x(2,2).a = 4;
+%! assert (struct2array (x), [1 3 2 4]);
+%! assert (size(struct2array (x)), [1 4]);
+
+%!test
+%! x(1).a = 1;
+%! x(3).a = 3;
+%! assert (struct2array (x), [1 3]);
+%! assert (size(struct2array (x)), [1 2]);
+
+%!test
+%! x(1,1).a = [1;2];
+%! x(1,2).a = [3;4];
+%! x(2,1).a = [5;6];
+%! x(2,2).a = [7;8];
+%! assert (struct2array (x), [1 5 3 7; 2 6 4 8]);
+%! assert (size(struct2array (x)), [2 4]);
+
+
+## Test input validation
+%!error struct2array ()
+%!error struct2array ([])
+%!error struct2array(NaN)
+%!error struct2array ([1 2])
+%!error struct2array ({[1 2]})
+%!error struct2array (struct ('foo', [1;2], 'bar', 3))
+%!error struct2array (struct ('foo', [1;2], 'bar', 'blah'))
+%!error struct2array (struct ('a', 1, 'b', magic (3)))
+
