1 ## Copyright (C) 2010,2011 Philip Nienhuis <prnienhuis@users.sf.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} [ @var{type-array} ] = spsh_prstype ( @var{iarray}, @var{rowsize}, @var{colsize}, @var{celltypes}, @var{options})
18 ## (Internal function) Return rectangular array with codes for cell types in rectangular input cell array @var{iarray}.
19 ## Codes are contained in input vector in order of Numeric, Boolean, Text, Formula and Empty, resp.
21 ## spsh_prstype should not be invoked directly but rather through oct2xls or oct2ods.
26 ## typarr = spsh_chkrange (cellarray, nr, nc, ctypes, options);
31 ## Author: Philip Nienhuis, <prnienhuis@users.sf.net>
32 ## Created: 2010-08-02
34 ## 2010-08-25 Corrected help text (square -> rectangular; stressed "internal" use)
35 ## 2011-04-21 Formulas now don't need closing ")" (e.g., =A1+B1 is OK as well)
36 ## " Formula ptrs in output arg now OK (cellfun(@(x).... skips empty cells)
38 function [ typearr ] = spsh_prstype (obj, nrows, ncols, ctype, spsh_opts)
45 # 5 = error / NaN / empty
47 typearr = ctype(5) * ones (nrows, ncols); # type "EMPTY", provisionally
48 obj2 = cell (size (obj)); # Temporary storage for strings
50 txtptr = cellfun ('isclass', obj, 'char'); # type "STRING" replaced by "NUMERIC"
51 obj2(txtptr) = obj(txtptr); obj(txtptr) = ctype(3); # Save strings in a safe place
53 emptr = cellfun ("isempty", obj);
54 obj(emptr) = ctype(5); # Set empty cells to NUMERIC
56 lptr = cellfun ("islogical" , obj); # Find logicals...
57 obj2(lptr) = obj(lptr); # .. and set them to BOOLEAN
59 ptr = cellfun ("isnan", obj); # Find NaNs & set to BLANK
60 typearr(ptr) = ctype(5); typearr(~ptr) = ctype(1); # All other cells are now numeric
62 obj(txtptr) = obj2(txtptr); # Copy strings back into place
63 obj(lptr) = obj2(lptr); # Same for logicals
64 obj(emptr) = -1; # Add in a filler value for empty cells
66 typearr(txtptr) = ctype(3); # ...and clean up
67 typearr(emptr) = ctype(5); # EMPTY
68 typearr(lptr) = ctype(2); # BOOLEAN
70 if ~(spsh_opts.formulas_as_text)
71 # Find formulas (designated by a string starting with "=" and ending in ")")
72 #fptr = cellfun (@(x) ischar (x) && strncmp (x, "=", 1) && strncmp (x(end:end), ")", 1), obj);
73 # Find formulas (designated by a string starting with "=")
74 fptr = cellfun (@(x) ischar (x) && strncmp (x, "=", 1), obj);
75 typearr(fptr) = ctype(4); # FORMULA
81 %! tstobj = {1.5, true, []; 'Text1', '=A1+B1', '=SQRT(A1)'; NaN, {}, 0};
82 %! typarr = spsh_prstype (tstobj, 3, 3, [1 2 3 4 5], struct ("formulas_as_text", 0));
83 %! assert (typarr, [1 2 5; 3 4 4; 5 5 1]);
86 %! tstobj = {1.5, true, []; 'Text1', '=A1+B1', '=SQRT(A1)'; NaN, {}, 0};
87 %! typarr = spsh_prstype (tstobj, 3, 3, [1 2 3 4 5], struct ("formulas_as_text", 1));
88 %! assert (typarr, [1 2 5; 3 3 3; 5 5 1]);