PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/code_oth/vis_valuetype.m

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