changeset 11285:107e7476a5da

cell2mat.m: Return empty matrix for empty cell.
author Ben Abbott <bpabbott@mac.com>
date Sun, 21 Nov 2010 13:14:25 -0500
parents b9bc32327c4d
children f0478684bc2f
files scripts/ChangeLog scripts/general/cell2mat.m
diffstat 2 files changed, 17 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,7 @@
+2010-11-21  Ben Abbott <bpabbot@mac.com>
+
+	* general/cell2mat.m: Return empty matrix for empty cell.
+
 2010-11-21  Kai Habel  <kai.habel@gmx.de>
 
 	* (plot/uigetfile.m, plot/uiputfile.m): Set default directory to pwd
--- a/scripts/general/cell2mat.m
+++ b/scripts/general/cell2mat.m
@@ -38,20 +38,21 @@
   
   nb = numel (c);
 
-  ## We only want numeric, logical, and char matrices.
-  valid = cellfun (@isnumeric, c);
-  valid |= cellfun (@islogical, c);
-  valid |= cellfun (@ischar, c);
-  validc = cellfun (@iscell, c);
-  valids = cellfun (@isstruct, c);
-
-  if (! all (valid(:)) && ! all (validc(:)) && ! all (valids(:)))
-    error ("cell2mat: wrong type elements or mixed cells, structs and matrices");
-  endif
-
   if (nb == 0)
     m = [];
   else
+
+    ## We only want numeric, logical, and char matrices.
+    valid = cellfun (@isnumeric, c);
+    valid |= cellfun (@islogical, c);
+    valid |= cellfun (@ischar, c);
+    validc = cellfun (@iscell, c);
+    valids = cellfun (@isstruct, c);
+
+    if (! all (valid(:)) && ! all (validc(:)) && ! all (valids(:)))
+      error ("cell2mat: wrong type elements or mixed cells, structs and matrices");
+    endif
+
     ## The goal is to minimize the total number of cat() calls.
     ## The dimensions can be concatenated along in arbitrary order.
     ## The numbers of concatenations are:
@@ -96,6 +97,7 @@
 %!test
 %! m = {1, 2, 3};
 %! assert (cell2mat (mat2cell (m, 1, [1 1 1])), m);
+%!assert (cell2mat ({}), []);
 ## Demos
 %!demo
 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};