annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/vis_valuetype.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function flag=vis_valuetype(value, valid, str);
wolffd@0 2
wolffd@0 3 % VIS_VALUETYPE Used for type checks in SOM Toolbox visualization routines
wolffd@0 4 %
wolffd@0 5 % flag = vis_valuetype(value, valid, str)
wolffd@0 6 %
wolffd@0 7 % Input and output arguments:
wolffd@0 8 % value (varies) variable to be checked
wolffd@0 9 % valid (cell array) size 1xN, cells are strings or vectors (see below)
wolffd@0 10 % str (string) 'all' or 'any' (default), determines whether
wolffd@0 11 % all or just any of the types listed in argument 'valid'
wolffd@0 12 % should be true for 'value'
wolffd@0 13 %
wolffd@0 14 % flag (scalar) 1 or 0 (true or false)
wolffd@0 15 %
wolffd@0 16 % This is an internal function of SOM Toolbox visualization. It makes
wolffd@0 17 % various type checks. For example:
wolffd@0 18 %
wolffd@0 19 % % Return 1 if X is a numeric scalar otherwise 0:
wolffd@0 20 % f=vis_valuetype(X,{'1x1'});
wolffd@0 21 %
wolffd@0 22 % % Return 1 if X is a ColorSpec, that is, a 1x3 vector presenting an RGB
wolffd@0 23 % % value or any of strings 'red','blue','green','yellow','magenta','cyan',
wolffd@0 24 % % 'white' or 'black' or their shortenings 'r','g','b','y','m','c','w','k':
wolffd@0 25 % f=vis_valueype(X,{'1x3rgb','colorstyle'})
wolffd@0 26 %
wolffd@0 27 % % Return 1 if X is _both_ 10x3 size numeric matrix and has RGB values as rows
wolffd@0 28 % f=vis_valuetype(X,{'nx3rgb',[10 3]},'all')
wolffd@0 29 %
wolffd@0 30 % Strings that may be used in argument valid:
wolffd@0 31 % id is true if value is
wolffd@0 32 %
wolffd@0 33 % [n1 n2 ... nn] any n1 x n2 x ... x nn sized numeric matrix
wolffd@0 34 % '1x1' scalar (numeric)
wolffd@0 35 % '1x2' 1x2 vector (numeric)
wolffd@0 36 % 'nx1' any nx1 numeric vector
wolffd@0 37 % 'nx2' nx2
wolffd@0 38 % 'nx3' nx3
wolffd@0 39 % 'nxn' any numeric square matrix
wolffd@0 40 % 'nxn[0,1]' numeric square matrix with values in interval [0,1]
wolffd@0 41 % 'nxm' any numeric matrix
wolffd@0 42 % '1xn' any 1xn numeric vector
wolffd@0 43 % '1x3rgb' 1x3 vector v for which all(v>=0 & v<=1), e.g., a RGB code
wolffd@0 44 % 'nx3rgb' nx3 numeric matrix that contains n RGB values as rows
wolffd@0 45 % 'nx3dimrgb' nx3xdim numeric matrix that contains RGB values
wolffd@0 46 % 'nxnx3rgb' nxnx3 numeric matrix of nxn RGB triples
wolffd@0 47 % 'none' string 'none'
wolffd@0 48 % 'xor' string 'xor'
wolffd@0 49 % 'indexed' string 'indexed'
wolffd@0 50 % 'colorstyle' strings 'red','blue','green','yellow','magenta','cyan','white'
wolffd@0 51 % or 'black', or 'r','g','b','y','m','c','w','k'
wolffd@0 52 % 'markerstyle' any of Matlab's marker chars '.','o','x','+','*','s','d','v',
wolffd@0 53 % '^','<','>','p'or 'h'
wolffd@0 54 % 'linestyle' any or Matlab's line style strings '-',':','--', or '-.'
wolffd@0 55 % 'cellcolumn' a nx1 cell array
wolffd@0 56 % 'topol_cell' {lattice, msize, shape}
wolffd@0 57 % 'topol_cell_no_shape' {lattice, msize}
wolffd@0 58 % 'string' any string (1xn array of char)
wolffd@0 59 % 'chararray' any MxN char array
wolffd@0 60
wolffd@0 61 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
wolffd@0 62 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 63
wolffd@0 64 % Version 2.0beta Johan 201099 juuso 280800
wolffd@0 65
wolffd@0 66 if nargin == 2
wolffd@0 67 str='any';
wolffd@0 68 end
wolffd@0 69
wolffd@0 70 flag=0;
wolffd@0 71 sz=size(value);
wolffd@0 72 dims=ndims(value);
wolffd@0 73
wolffd@0 74 % isnumeric
wolffd@0 75 numeric=isnumeric(value);
wolffd@0 76 character=ischar(value);
wolffd@0 77
wolffd@0 78 % main loop: go through all types in arg. 'valid'
wolffd@0 79 for i=1:length(valid),
wolffd@0 80 if isnumeric(valid{i}), % numeric size for double matrix
wolffd@0 81 if numeric & length(valid{i}) == dims,
wolffd@0 82 flag(i)=all(sz == valid{i});
wolffd@0 83 else
wolffd@0 84 flag(i)=0; % not numeric or wrong dimension
wolffd@0 85 end
wolffd@0 86 else
wolffd@0 87 msg=''; % for a error message inside try
wolffd@0 88 try
wolffd@0 89 switch valid{i}
wolffd@0 90
wolffd@0 91 % scalar
wolffd@0 92 case '1x1'
wolffd@0 93 flag(i)=numeric & dims == 2 & sz(1)==1 & sz(2) ==1;
wolffd@0 94
wolffd@0 95 % 1x2 numeric vector
wolffd@0 96 case '1x2'
wolffd@0 97 flag(i)=numeric & dims == 2 & sz(1)==1 & sz(2) == 2;
wolffd@0 98
wolffd@0 99 % 1xn numeric vector
wolffd@0 100 case '1xn'
wolffd@0 101 flag(i)=numeric & dims == 2 & sz(1) == 1;
wolffd@0 102
wolffd@0 103 % any numeric matrix
wolffd@0 104 case 'nxm'
wolffd@0 105 flag(i)=numeric & dims == 2;
wolffd@0 106
wolffd@0 107 % nx3 numeric matrix
wolffd@0 108 case 'nx3'
wolffd@0 109 flag(i)=numeric & dims == 2 & sz(2) == 3;
wolffd@0 110
wolffd@0 111 % nx2 numeric matrix
wolffd@0 112 case 'nx2'
wolffd@0 113 flag(i)=numeric & dims == 2 & sz(2) == 2;
wolffd@0 114
wolffd@0 115 % nx1 numeric vector
wolffd@0 116 case 'nx1'
wolffd@0 117 flag(i)=numeric & dims == 2 & sz(2) == 1;
wolffd@0 118
wolffd@0 119 % nx1xm numric matrix
wolffd@0 120 case 'nx1xm'
wolffd@0 121 flag(i)=numeric & dims == 3 & sz(2) == 1;
wolffd@0 122
wolffd@0 123 % nx3 matrix of RGB triples
wolffd@0 124 case 'nx3rgb'
wolffd@0 125 flag(i)=numeric & dims == 2 & sz(2) == 3 & in0_1(value);
wolffd@0 126
wolffd@0 127 % RGB triple (ColorSpec vector)
wolffd@0 128 case '1x3rgb'
wolffd@0 129 flag(i) = numeric & dims == 2 & sz(1)==1 & sz(2) == 3 & in0_1(value);
wolffd@0 130
wolffd@0 131 % any square matrix
wolffd@0 132 case 'nxn'
wolffd@0 133 flag(i)=numeric & dims == 2 & sz(1) == sz(2);
wolffd@0 134
wolffd@0 135 % nx3xdim array of nxdim RGB triples
wolffd@0 136 case 'nx3xdimrgb'
wolffd@0 137 flag(i)=numeric & dims == 3 & sz(2) == 3 & in0_1(value);
wolffd@0 138
wolffd@0 139 % nxnx3 array of nxn RGB triples
wolffd@0 140 case 'nxnx3rgb'
wolffd@0 141 flag(i)= numeric & dims == 3 & sz(1) == sz(2) & sz(3) == 3 ...
wolffd@0 142 & in0_1(value);
wolffd@0 143
wolffd@0 144 % nxn matrix of values between [0,1]
wolffd@0 145 case 'nxn[0,1]'
wolffd@0 146
wolffd@0 147 flag(i)=numeric & dims == 2 & sz(1) == sz(2) & in0_1(value);
wolffd@0 148
wolffd@0 149 % string 'indexed'
wolffd@0 150 case 'indexed'
wolffd@0 151 flag(i) = ischar(value) & strcmp(value,'indexed');
wolffd@0 152
wolffd@0 153 % string 'none'
wolffd@0 154 case 'none'
wolffd@0 155 flag(i) = character & strcmp(value,'none');
wolffd@0 156
wolffd@0 157 % string 'xor'
wolffd@0 158 case 'xor'
wolffd@0 159 flag(i) = character & strcmp(value,'xor');
wolffd@0 160
wolffd@0 161 % any string (1xn char array)
wolffd@0 162 case 'string'
wolffd@0 163 flag(i) = character & dims == 2 & sz(1)<=1;
wolffd@0 164
wolffd@0 165 % any char array
wolffd@0 166 case 'chararray'
wolffd@0 167 flag(i) = character & dims == 2 & sz(1)>0;
wolffd@0 168
wolffd@0 169 % ColorSpec string
wolffd@0 170 case 'colorstyle'
wolffd@0 171 flag(i)=(character & sz(1) == 1 & sz(2) == 1 & ...
wolffd@0 172 any(ismember('ymcrgbwk',value))) | ...
wolffd@0 173 (ischar(value) & any(strcmp(value,{'none','yellow','magenta',...
wolffd@0 174 'cyan','red','green','blue','white','black'})));
wolffd@0 175
wolffd@0 176 % any valid Matlab's Marker
wolffd@0 177 case 'markerstyle'
wolffd@0 178 flag(i)=character & sz(1) == 1 & sz(2) == 1 & ...
wolffd@0 179 any(ismember('.ox+*sdv^<>ph',value));
wolffd@0 180
wolffd@0 181 % any valid Matlab's LineStyle
wolffd@0 182 case 'linestyle'
wolffd@0 183 str=strrep(strrep(strrep(value,'z','1'),'--','z'),'-.','z');
wolffd@0 184 flag(i)=character & any(ismember(str,'z-:')) & sz(1)==1 & (sz(2)==1 | sz(2)==2);
wolffd@0 185
wolffd@0 186 % any struct
wolffd@0 187 case 'struct'
wolffd@0 188 flag(i)=isstruct(value);
wolffd@0 189
wolffd@0 190 % nx1 cell array of strings
wolffd@0 191 case 'cellcolumn_of_char'
wolffd@0 192 flag(i)=iscell(value) & dims == 2 & sz(2)==1;
wolffd@0 193 try, char(value); catch, flag(i)=0; end
wolffd@0 194
wolffd@0 195 % mxn cell array of strings
wolffd@0 196 case '2Dcellarray_of_char'
wolffd@0 197 flag(i)=iscell(value) & dims == 2;
wolffd@0 198 try, char(cat(2,value{:})); catch, flag(i)=0; end
wolffd@0 199
wolffd@0 200 % valid {lattice, msize}
wolffd@0 201 case 'topol_cell_no_shape'
wolffd@0 202 flag(i)=1;
wolffd@0 203 if ~iscell(value) | length(size(value)) ~= 2 | size(value,2)~=2
wolffd@0 204 flag(i)=0;
wolffd@0 205 else
wolffd@0 206 if vis_valuetype(value{1},{'string'}),
wolffd@0 207 switch value{1}
wolffd@0 208 case { 'hexa','rect'}
wolffd@0 209 ;
wolffd@0 210 otherwise
wolffd@0 211 flag(i)=0;
wolffd@0 212 end
wolffd@0 213 end
wolffd@0 214 if ~vis_valuetype(value{2},{'1xn'}),
wolffd@0 215 flag(i)=0;
wolffd@0 216 end
wolffd@0 217 end
wolffd@0 218
wolffd@0 219 % valid {lattice, msize, shape}
wolffd@0 220 case 'topol_cell'
wolffd@0 221 flag(i)=1;
wolffd@0 222 if ~iscell(value) | length(size(value)) ~= 2 | size(value,2) ~= 3,
wolffd@0 223 flag(i)=0;
wolffd@0 224 else
wolffd@0 225 if vis_valuetype(value{1},{'string'}),
wolffd@0 226 switch value{1}
wolffd@0 227 case { 'hexa','rect'}
wolffd@0 228 ;
wolffd@0 229 otherwise
wolffd@0 230 flag(i)=0;
wolffd@0 231 end
wolffd@0 232 end
wolffd@0 233 if ~vis_valuetype(value{2},{'1xn'})
wolffd@0 234 flag(i)=0;
wolffd@0 235 end
wolffd@0 236 if ~vis_valuetype(value{3},{'string'})
wolffd@0 237 flag(i)=0;
wolffd@0 238 else
wolffd@0 239 switch value{3}
wolffd@0 240 case { 'sheet','cyl', 'toroid'}
wolffd@0 241 ;
wolffd@0 242 otherwise
wolffd@0 243 flag(i)=0;
wolffd@0 244 end
wolffd@0 245 end
wolffd@0 246 end
wolffd@0 247 otherwise
wolffd@0 248 msg='Unknown valuetype!';
wolffd@0 249 end
wolffd@0 250 catch
wolffd@0 251 % error during type check is due to wrong type of value:
wolffd@0 252 % lets set flag(i) to 0
wolffd@0 253 flag(i)=0;
wolffd@0 254 end
wolffd@0 255 % Unknown indetifier?
wolffd@0 256 error(msg);
wolffd@0 257 end
wolffd@0 258 % set flag according to 3rd parameter (all ~ AND, any ~ OR)
wolffd@0 259 if strcmp(str,'all');
wolffd@0 260 flag=all(flag);
wolffd@0 261 else
wolffd@0 262 flag=any(flag);
wolffd@0 263 end
wolffd@0 264 end
wolffd@0 265
wolffd@0 266
wolffd@0 267 function f=in0_1(value)
wolffd@0 268
wolffd@0 269 f=all(value(:) >= 0 & value(:)<=1);