Mercurial > hg > octave-avbm
changeset 13868:87f78c11d725
cell2struct: if DIM is omitted, use a default value of 1.
* ov-struct.cc (Fcell2struct): Provide default value for DIM argument.
Reorganize argument decoding. New test.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 15 Nov 2011 01:29:22 -0500 |
parents | 922bfdd80413 |
children | bdad80f28d5c adf60d6dc1dd |
files | src/graphics.h.in src/ov-struct.cc |
diffstat | 2 files changed, 78 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/src/graphics.h.in +++ b/src/graphics.h.in @@ -4694,7 +4694,7 @@ { position.add_constraint (dim_vector (1, 2)); position.add_constraint (dim_vector (2, 1)); - visible.set (octave_value (false)); + visible.set (octave_value (true)); } };
--- a/src/ov-struct.cc +++ b/src/ov-struct.cc @@ -2029,6 +2029,7 @@ Convert @var{cell} to a structure. The number of fields in @var{fields}\n\ must match the number of elements in @var{cell} along dimension @var{dim},\n\ that is @code{numel (@var{fields}) == size (@var{cell}, @var{dim})}.\n\ +If @var{dim} is omitted, a value of 1 is assumed.\n\ \n\ @example\n\ @group\n\ @@ -2048,63 +2049,87 @@ { octave_value retval; - if (args.length () == 3) + int nargin = args.length (); + + if (nargin == 2 || nargin == 3) { if (! args(0).is_cell ()) - error ("cell2struct: argument CELL must be of type cell"); - else if (! (args(1).is_cellstr () || args(1).is_char_matrix ())) - error ("cell2struct: FIELDS must be a cell array of strings or a character matrix"); - else if (! args(2).is_real_scalar ()) - error ("cell2struct: DIM must be a real scalar"); - else + { + error ("cell2struct: argument CELL must be of type cell"); + return retval; + } + + if (! (args(1).is_cellstr () || args(1).is_char_matrix ())) { - const Cell vals = args(0).cell_value (); - const Array<std::string> fields = args(1).cellstr_value (); - int dim = args(2).int_value () - 1; - octave_idx_type ext = 0; - - if (dim < 0) - error ("cell2struct: DIM must be a valid dimension"); + error ("cell2struct: FIELDS must be a cell array of strings or a character matrix"); + return retval; + } + + const Cell vals = args(0).cell_value (); + const Array<std::string> fields = args(1).cellstr_value (); + + octave_idx_type ext = 0; + + int dim = 0; + + if (nargin == 3) + { + if (args(2).is_real_scalar ()) + { + dim = nargin == 2 ? 0 : args(2).int_value () - 1; + + if (error_state) + return retval; + } else { - ext = vals.ndims () > dim ? vals.dims ()(dim) : 1; - if (ext != fields.numel ()) - error ("cell2struct: number of FIELDS does not match dimension"); - } - - - if (! error_state) - { - int nd = std::max (dim+1, vals.ndims ()); - // result dimensions. - dim_vector rdv = vals.dims ().redim (nd); - - assert (ext == rdv(dim)); - if (nd == 2) - { - rdv(0) = rdv(1-dim); - rdv(1) = 1; - } - else - { - for (int i = dim + 1; i < nd; i++) - rdv(i-1) = rdv(i); - - rdv.resize (nd-1); - } - - octave_map map (rdv); - Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon); - - for (octave_idx_type i = 0; i < ext; i++) - { - ia(dim) = i; - map.setfield (fields(i), vals.index (ia).reshape (rdv)); - } - - retval = map; + error ("cell2struct: DIM must be a real scalar"); + return retval; } } + + if (dim < 0) + { + error ("cell2struct: DIM must be a valid dimension"); + return retval; + } + + ext = vals.ndims () > dim ? vals.dims ()(dim) : 1; + + if (ext != fields.numel ()) + { + error ("cell2struct: number of FIELDS does not match dimension"); + return retval; + } + + int nd = std::max (dim+1, vals.ndims ()); + // result dimensions. + dim_vector rdv = vals.dims ().redim (nd); + + assert (ext == rdv(dim)); + if (nd == 2) + { + rdv(0) = rdv(1-dim); + rdv(1) = 1; + } + else + { + for (int i = dim + 1; i < nd; i++) + rdv(i-1) = rdv(i); + + rdv.resize (nd-1); + } + + octave_map map (rdv); + Array<idx_vector> ia (dim_vector (nd, 1), idx_vector::colon); + + for (octave_idx_type i = 0; i < ext; i++) + { + ia(dim) = i; + map.setfield (fields(i), vals.index (ia).reshape (rdv)); + } + + retval = map; } else print_usage (); @@ -2122,6 +2147,8 @@ %! assert (s, t); %! assert (struct2cell (s), vals'); %! assert (fieldnames (s), keys'); + +%!assert (cell2struct ({1; 2}, {"a"; "b"}), struct ("a", 1, "b", 2)); */