changeset 19246:b85d4930528d

substruct.m: Overhaul function. * substruct.m: Redo docstring. Place input validation first in function.
author Rik <rik@octave.org>
date Sat, 04 Oct 2014 19:36:41 -0700
parents bbb1fbd900d4
children d627d9c8adfd
files scripts/miscellaneous/substruct.m
diffstat 1 files changed, 25 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/substruct.m	Sat Oct 04 19:34:30 2014 -0700
+++ b/scripts/miscellaneous/substruct.m	Sat Oct 04 19:36:41 2014 -0700
@@ -19,8 +19,9 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{})
-## Create a subscript structure for use with @code{subsref} or
-## @code{subsasgn}.  For example:
+## Create a subscript structure for use with @code{subsref} or @code{subsasgn}.
+##
+## For example:
 ##
 ## @example
 ## @group
@@ -35,7 +36,9 @@
 ##            [1,2] = :
 ##          @}
 ##        @}
-## x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
+## x = [1, 2, 3;
+##      4, 5, 6;
+##      7, 8, 9];
 ## subsref (x, idx)
 ##    @result{} 7  8  9
 ## @end group
@@ -47,30 +50,28 @@
 
 function retval = substruct (varargin)
 
-  nargs = nargin;
-
-  if (nargs > 1 && mod (nargs, 2) == 0)
-    typ = varargin(1:2:nargs);
-    sub = varargin(2:2:nargs);
-    braces = strcmp (typ, "()") | strcmp (typ, "{}");
-    dots = strcmp (typ, ".");
-    if (all (braces | dots))
-      cells = cellfun ("isclass", sub, "cell");
-      chars = cellfun ("isclass", sub, "char");
-      if (any (braces &! cells))
-        error ("substruct: for TYPE == () or {}, SUBS must be a cell array");
-      elseif (any (dots &! chars))
-        error ("substruct: for TYPE == ., SUBS must be a character string");
-      endif
-    else
-      error ("substruct: expecting TYPE to be one of \"()\", \"{}\", or \".\"");
-    endif
-
-    retval = struct ("type", typ, "subs", sub);
-  else
+  if (nargin < 2 || mod (nargin, 2) != 0)
     print_usage ();
   endif
 
+  typ = varargin(1:2:nargin);
+  sub = varargin(2:2:nargin);
+  braces = strcmp (typ, "()") | strcmp (typ, "{}");
+  dots = strcmp (typ, ".");
+  if (all (braces | dots))
+    cells = cellfun ("isclass", sub, "cell");
+    chars = cellfun ("isclass", sub, "char");
+    if (any (braces & !cells))
+      error ("substruct: for TYPE == () or {}, SUBS must be a cell array");
+    elseif (any (dots & !chars))
+      error ("substruct: for TYPE == ., SUBS must be a character string");
+    endif
+  else
+    error ('substruct: expecting TYPE to be one of "()", "{}", or "."');
+  endif
+
+  retval = struct ("type", typ, "subs", sub);
+
 endfunction