]> Creatis software - CreaPhase.git/blob - octave_packages/io-1.0.19/spsh_prstype.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / io-1.0.19 / spsh_prstype.m
1 ## Copyright (C) 2010,2011 Philip Nienhuis <prnienhuis@users.sf.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
20 ##
21 ## spsh_prstype should not be invoked directly but rather through oct2xls or oct2ods.
22 ##
23 ## Example:
24 ##
25 ## @example
26 ##   typarr = spsh_chkrange (cellarray, nr, nc, ctypes, options);
27 ## @end example
28 ##
29 ## @end deftypefn
30
31 ## Author: Philip Nienhuis, <prnienhuis@users.sf.net>
32 ## Created: 2010-08-02
33 ## Updates:
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)
37
38 function [ typearr ] = spsh_prstype (obj, nrows, ncols, ctype, spsh_opts)
39
40         # ctype index:
41         # 1 = numeric
42         # 2 = boolean
43         # 3 = text
44         # 4 = formula 
45         # 5 = error / NaN / empty
46
47         typearr = ctype(5) * ones (nrows, ncols);                       # type "EMPTY", provisionally
48         obj2 = cell (size (obj));                                                       # Temporary storage for strings
49
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
52
53         emptr = cellfun ("isempty", obj);
54         obj(emptr) = ctype(5);                                                          # Set empty cells to NUMERIC
55
56         lptr = cellfun ("islogical" , obj);             # Find logicals...
57         obj2(lptr) = obj(lptr);                                                         # .. and set them to BOOLEAN
58
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
61
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
65
66         typearr(txtptr) = ctype(3);                                             # ...and clean up 
67         typearr(emptr) = ctype(5);                                              # EMPTY
68         typearr(lptr) = ctype(2);                                                       # BOOLEAN
69
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
76         endif
77
78 endfunction
79
80 %!test
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]);
84
85 %!test
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]);