annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_grid.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [S,m,l,t,s]=som_grid(varargin)
Daniel@0 2
Daniel@0 3 %SOM_GRID Visualization of a SOM grid
Daniel@0 4 %
Daniel@0 5 % [sGrid,m,l,t,s]=som_grid(sGrid, ['argID', value, ...])
Daniel@0 6 % [sGrid,m,l,t,s]=som_grid(topol, ['argID', value, ...])
Daniel@0 7 % [sGrid,m,l,t,s]=som_grid(lattice, msize, ['argID', value, ...])
Daniel@0 8 %
Daniel@0 9 % Input and output arguments ([]'s are optional)
Daniel@0 10 % sGrid (struct) som_grid struct (see output arguments)
Daniel@0 11 % topol (struct) map or topol struct for giving the topology
Daniel@0 12 % (cell array) of form {'lattice', msize, ['shape']}.
Daniel@0 13 % Default value for 'shape' is 'sheet'.
Daniel@0 14 % lattice (string) 'hexa', 'rect'
Daniel@0 15 % (matrix) size M x M, defines topological connections
Daniel@0 16 % msize (vector) 1x2 vector defines the grid size, M=msize(1)*msize(2)
Daniel@0 17 % ['argID',(string) Other arguments can be given as 'argID', value
Daniel@0 18 % value] (varies) pairs. See list below for valid values.
Daniel@0 19 %
Daniel@0 20 % sGrid (struct) with fields S.msize, S.shape, S.lattice, S.coord, S.marker,
Daniel@0 21 % S.markersize, S.markercolor, S.line, S.linewidth, S.linecolor,
Daniel@0 22 % S.surf, S.label, S.labelsize, S.labelcolor
Daniel@0 23 % m (matrix) handels to LINE objects (unit markers)
Daniel@0 24 % l (matrix) handles to LINE objects (lines connecting the units)
Daniel@0 25 % t (matrix) handles to TEXT objects (labels)
Daniel@0 26 % s (scalar) handle to SURF object (surface between units)
Daniel@0 27 %
Daniel@0 28 % Here are the valid argument IDs (case insensitive) and
Daniel@0 29 % associated values:
Daniel@0 30 % 'Coord' Mx2 or Mx3 matrix of coordinates
Daniel@0 31 % (default: according to lattice as in som_cplane)
Daniel@0 32 % 'Marker' string 'o','+','x','*','v','^','<','>','h','s','d','p','.',
Daniel@0 33 % 'none' or Mx1 cell or char array of these strings
Daniel@0 34 % Default: 'o'.
Daniel@0 35 % 'MarkerSize' scalar or Mx1 matrix of double. Default: 6.
Daniel@0 36 % 'MarkerColor' ColorSpec or Mx3 matrix of RGB triples. Default: 'k'.
Daniel@0 37 % 'Line' string '-',':','--' or '-.' or 'none'. Default: '-'.
Daniel@0 38 % 'Surf' [], Mx1 or Mx3 matrix of RGB triples
Daniel@0 39 % to define surface values. Default: [] = no surf.
Daniel@0 40 % Note: shading is turned to 'interp'.
Daniel@0 41 % 'LineWidth' scalar or MxM matrix, default: 0.5
Daniel@0 42 % 'LineColor' ColorSepc, MxMx3 matrix of RGB triples or a cell array
Daniel@0 43 % of form {r g b} where r,g, and b are MxM
Daniel@0 44 % (sparse) matrices of R,G, and B values
Daniel@0 45 % 'Label' Mx1 char array, cell array of strings size MxL
Daniel@0 46 % or [] to indicate no labels, default: [] = no labels.
Daniel@0 47 % 'LabelSize' scalar
Daniel@0 48 % 'LabelColor' ColorSpec or string 'none', default: 'g'.
Daniel@0 49 %
Daniel@0 50 % For more help, try 'type som_grid' or check out online documentation.
Daniel@0 51 % See also SOM_CONNECTION, SOM_SHOW, SOM_CPLANE, SOM_SET, SCATTER, SCATTER3.
Daniel@0 52
Daniel@0 53 %%%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 54 %
Daniel@0 55 % som_grid
Daniel@0 56 %
Daniel@0 57 % PURPOSE
Daniel@0 58 %
Daniel@0 59 % To visualize the SOM grid in various ways
Daniel@0 60 %
Daniel@0 61 % SYNTAX
Daniel@0 62 %
Daniel@0 63 % [sGrid,m,l,t,s]=som_grid(sGrid)
Daniel@0 64 % [sGrid,m,l,t,s]=som_grid(sTopol)
Daniel@0 65 % [sGrid,m,l,t,s]=som_grid(sMap)
Daniel@0 66 % [sGrid,m,l,t,s]=som_grid({lattice, msize, [shape]})
Daniel@0 67 % [sGrid,m,l,t,s]=som_grid(lattice, msize)
Daniel@0 68 % [sGrid,m,l,t,s]=som_grid(..., ['argID', value, ...])
Daniel@0 69 %
Daniel@0 70 % DESCRIPTION
Daniel@0 71 %
Daniel@0 72 % The SOM can be defined as a set of units (neurons) and their
Daniel@0 73 % topological relations. This function is used to visualize these in
Daniel@0 74 % various ways. The units may be drawn using different markers and
Daniel@0 75 % colors, in different sizes and in different locations in 2D or
Daniel@0 76 % 3D. However the topological neighborhood is limited to be
Daniel@0 77 % 2-dimensional. The connections between these units may be drawn using
Daniel@0 78 % lines having different thicknesses and colors. Labeling text may be
Daniel@0 79 % plotted on the units. It is possible also to draw a surface between
Daniel@0 80 % the units. The surface coloring is either indexed (one value per
Daniel@0 81 % unit) or fixed RGB (a 1x3 RGB triple per unit).
Daniel@0 82 %
Daniel@0 83 % REQUIRED INPUT ARGUMENTS
Daniel@0 84 %
Daniel@0 85 % Note: M is the number of map units.
Daniel@0 86 %
Daniel@0 87 % The first (or first two) argument may have various different types of values
Daniel@0 88 %
Daniel@0 89 % 1. sGrid (struct) som_grid struct (the output of this function)
Daniel@0 90 %
Daniel@0 91 % The struct initiates the visualization. The argID-value -pairs
Daniel@0 92 % are used to alter the initiation.
Daniel@0 93 %
Daniel@0 94 % Following argument types may be used to give the topology for the grid
Daniel@0 95 %
Daniel@0 96 % 2. sTopol (struct) som_topol struct
Daniel@0 97 % 3. sMap (struct) som_map struct (only topology matters)
Daniel@0 98 % 4. {lattice, msize} or {lattice, msize, sheet} (cell array)
Daniel@0 99 % - lattice must be 'hexa' or 'rect'
Daniel@0 100 % - msize must be a 1x2 vector
Daniel@0 101 % - shape (if specified) must be string 'sheet', 'cyl' or 'toroid'
Daniel@0 102 % If shape is not given it is 'sheet' by default.
Daniel@0 103 % 5. lattice (string or matrix) AND msize (1x2 vector) as two separate arguments
Daniel@0 104 % - lattice may be string 'rect' or 'hexa' or a connection matrix
Daniel@0 105 % (see SOM_CONNECTION) to define a free topology. This connection
Daniel@0 106 % matrix is of size MxM and its element i,j (i<j) is set
Daniel@0 107 % to 1 if there is a connection between units i and j, otherwise to
Daniel@0 108 % zero. Shape is set to 'sheet' by default. Shape does not have any
Daniel@0 109 % meaning if a free topology is specified, anyway.
Daniel@0 110 % - msize must be a 1x2 vector
Daniel@0 111 %
Daniel@0 112 % In cases 2...5 the sGrid structure is initiated by default values
Daniel@0 113 % which are set in SOM_SET. These include black markers 'o' (6pt),
Daniel@0 114 % light gray conncection lines (graph edges), unit coordinates
Daniel@0 115 % according to the lattice ('hexa','rect'), no labels, and no
Daniel@0 116 % surface.
Daniel@0 117 %
Daniel@0 118 % OPTIONAL INPUT ARGUMENTS
Daniel@0 119 %
Daniel@0 120 % Note: M is the number of map units.
Daniel@0 121 %
Daniel@0 122 % Here is a list of the valid arguments IDs and the associated
Daniel@0 123 % values (identifiers are case insensitive):
Daniel@0 124 %
Daniel@0 125 % 'Coord' Unit coordinates
Daniel@0 126 % This defines the coordinates of the units. Default: the
Daniel@0 127 % topological coordinates (calculated as in function
Daniel@0 128 % SOM_VIS_COORDS and SOM_CPLANE). If the topology is free
Daniel@0 129 % (lattice is a connection matrix) this argument is obligatory!
Daniel@0 130 % (matrix) size Mx2 of 2D coordinates for each unit
Daniel@0 131 % (matrix) size Mx3 of 3D coordinates for each unit
Daniel@0 132 %
Daniel@0 133 % 'Marker' Unit markers, default is 'o'.
Daniel@0 134 % (string) 'o','+','x','*','v','^','<','>','h','s','d', 'p','.', or 'none'
Daniel@0 135 % give the same marker for each unit.
Daniel@0 136 % (cell array) of size Mx1 of previous strings gives individual
Daniel@0 137 % markers for each unit.
Daniel@0 138 %
Daniel@0 139 % 'MarkerSize' Size (pt) of unit markers, default is 6 (pt).
Daniel@0 140 % (scalar) gives the same size for every unit.
Daniel@0 141 % (matrix) Mx1 gives an individual size for each unit marker.
Daniel@0 142 %
Daniel@0 143 % 'MarkerColor' Unit marker colors, default is 'k'
Daniel@0 144 % (ColorSpec) gives the same color each unit.
Daniel@0 145 % (matrix) Mx3 of RGB triples gives individual color for each unit
Daniel@0 146 % Note that indexed coloring - like in SOM_CPLANE - is
Daniel@0 147 % not possible. If indexed coloring is needed, you can
Daniel@0 148 % use SOM_NORMCOLOR to calculate RGB colors that
Daniel@0 149 % emulate indexed coloring. However, the colors for the
Daniel@0 150 % units are fixed, so changing colormap will not
Daniel@0 151 % change the colors.
Daniel@0 152 %
Daniel@0 153 % 'Line' Line type, default is '-'.
Daniel@0 154 % (string) '-',':','--' or '-.' or 'none'. Only one linetype in
Daniel@0 155 % grid is allowed.
Daniel@0 156 %
Daniel@0 157 % 'LineWidth' Width of the topological connection lines (edges)
Daniel@0 158 % (scalar) gives the same width for each line. Default is 0.5.
Daniel@0 159 % (matrix) MxM sparse (or full) matrix gives individual width for
Daniel@0 160 % each connection. The element (i,j), i<j, gives the line width for
Daniel@0 161 % connection between nodes i and j. (The sparse form is
Daniel@0 162 % recommended for saving memory, a full matrix works as well,
Daniel@0 163 % of course). Note that only the elements satisfying i<j
Daniel@0 164 % matter - as the elememts for which j >= i are ignored in
Daniel@0 165 % order to avoid ambiguous situations if the matrix would be
Daniel@0 166 % non-symmetric. The "connections to oneself" is not drawn.
Daniel@0 167 %
Daniel@0 168 % Line width zero is valid and causes the line to disappear.
Daniel@0 169 %
Daniel@0 170 % 'LineColor' Color of connection lines, default is [0.9 0.9 0.9].
Daniel@0 171 % (ColorSpec) gives the same color for each line
Daniel@0 172 % (matrix) MxMx3 matrix of RGB triples gives individual width for
Daniel@0 173 % each connection. The element (i,j,:), i<j, gives the RGB triple for
Daniel@0 174 % line between nodes i and j.
Daniel@0 175 % (cell array) of three sparse (or full) matrices {r,g,b} where
Daniel@0 176 % r(i,j), g(i,j) and b(i,j) gives the R,G, and B values in the RGB
Daniel@0 177 % triple for the line between nodes i and j. (The motivation for this
Daniel@0 178 % form is the fact that a 3D arrays can't use sparse format in
Daniel@0 179 % Matlab version 5.1.)
Daniel@0 180 %
Daniel@0 181 % Note that only the elements satisfying i<j matter - the elememts
Daniel@0 182 % for which j >= i are ignored in order to avoid ambiguous situations
Daniel@0 183 % if the matrix was non-symmetric. The "connections to oneself"
Daniel@0 184 % is not drawn.
Daniel@0 185 %
Daniel@0 186 %
Daniel@0 187 % 'Label' Labels for units, default is [].
Daniel@0 188 % (empty) [] means no labels.
Daniel@0 189 % (char array) of size Mx1. Element (i,:) has the label for unit i.
Daniel@0 190 % (cell array) of size MxL consisting of sets of labels. Element {i,:}
Daniel@0 191 % contains the labeling for unit i.
Daniel@0 192 % In case of multiple labels, the labels for one unit are shown
Daniel@0 193 % in one column centered at that unit.
Daniel@0 194 %
Daniel@0 195 % 'LabelSize' Text size of labels (points), default is 10.
Daniel@0 196 % (scalar) Default is 10.
Daniel@0 197 %
Daniel@0 198 % 'LabelColor' Color of labels, default is 'c' (cyan).
Daniel@0 199 % (ColorSpec) gives the same color for each label string 'xor'
Daniel@0 200 % sets the colors automatically so that they differ
Daniel@0 201 % from the background (using Matlab's built-in xor-color feature.)
Daniel@0 202 %
Daniel@0 203 % 'Surf' Surface between nodes, default is [].
Daniel@0 204 % (empty) [] gives no surface
Daniel@0 205 % (vector) Mx1 gives an indexed interpolated color surface between
Daniel@0 206 % units using the actual colormap.
Daniel@0 207 % (matrix) Mx3 matrix of RGB triples gives a interpolated color surface
Daniel@0 208 % between units using fixed RGB colors.
Daniel@0 209 %
Daniel@0 210 % Note that the interpolation is done using Matlab's built-in
Daniel@0 211 % color interpolation for SURF objects.
Daniel@0 212 %
Daniel@0 213 % OUTPUT ARGUMENTS
Daniel@0 214 %
Daniel@0 215 % sGrid (struct) with fields S.msize, S.shape, S.lattice, S.coord, S.marker,
Daniel@0 216 % S.markersize, S.markercolor, S.line, S.linewidth, S.linecolor,
Daniel@0 217 % S.surf, S.label, S.labelsize, S.labelcolor
Daniel@0 218 %
Daniel@0 219 % m (matrix) handels to LINE objects (unit markers)
Daniel@0 220 %
Daniel@0 221 % l (matrix) handles to LINE objects (lines connecting the units)
Daniel@0 222 %
Daniel@0 223 % t (matrix) handles to TEXT objects (labels)
Daniel@0 224 %
Daniel@0 225 % s (scalar) handle to SURF object (surface between units)
Daniel@0 226 %
Daniel@0 227 % EXAMPLES
Daniel@0 228 %
Daniel@0 229 % % Make map of size 15x10 on random data:
Daniel@0 230 %
Daniel@0 231 % map=som_make(rand(1000,4),'msize',[15 10], 'lattice', 'hexa');
Daniel@0 232 %
Daniel@0 233 % % Draw the grid using two frist varable values as coordinates
Daniel@0 234 % % and store the sGrid struct in varable S:
Daniel@0 235 %
Daniel@0 236 % S=som_grid(map, 'coord', map.codebook(:,[1 2]))
Daniel@0 237 %
Daniel@0 238 % %Define some things:
Daniel@0 239 % %
Daniel@0 240 % % Create a cell array of size 150x1 that divides map in to two label classes
Daniel@0 241 % % 'circles' and 'squares'
Daniel@0 242 %
Daniel@0 243 % L(1:75,1)='o'; L(76:150,1)='s'; L = cellstr(L);
Daniel@0 244 %
Daniel@0 245 % % Create a coloring according to the 3rd variable according to current
Daniel@0 246 % % colormap:
Daniel@0 247 %
Daniel@0 248 % C = som_normcolor(map.codebook(:,3));
Daniel@0 249 %
Daniel@0 250 % % Change the visualization: use black lines, unit markers in M and unit
Daniel@0 251 % % color in C, and set unit size to 10:
Daniel@0 252 %
Daniel@0 253 % S=som_grid(S, 'linecolor', 'k', 'marker', L, 'MarkerColor',C, ...
Daniel@0 254 % 'MarkerSize', 10);
Daniel@0 255 %
Daniel@0 256 % % Do a new visualization, use indexed color surface calcualted from the
Daniel@0 257 % % first variable, coordinates according to the lattice (default) but
Daniel@0 258 % % no markers nor lines:
Daniel@0 259 %
Daniel@0 260 % S=som_grid(map,'line','none','marker','none','surf',map.codebook(:,1));
Daniel@0 261 %
Daniel@0 262 % % Set coordinates according to three last varables
Daniel@0 263 %
Daniel@0 264 % som_grid(S,'coord',map.codebook(:,2:4));
Daniel@0 265 %
Daniel@0 266 % % Create a random connection matrix R1 and the usual hexagonal
Daniel@0 267 % % neighborhood connection matrix R2:
Daniel@0 268 %
Daniel@0 269 % R1=sparse(rand(150,150)>0.9);
Daniel@0 270 % R2=som_connection(map);
Daniel@0 271 %
Daniel@0 272 % % Show these connections. Note that coordinates _must_ now be given
Daniel@0 273 % % explicitly: we form default topological coordinates using
Daniel@0 274 % % som_unit_coords.
Daniel@0 275 %
Daniel@0 276 % som_grid(R1,map.topol.msize,'coord',som_unit_coords(map));
Daniel@0 277 % som_grid(R2,map.topol.msize,'coord',som_unit_coords(map));
Daniel@0 278 %
Daniel@0 279 % % Show connections (R1 AND R2)
Daniel@0 280 % som_grid(R2.*R2,map.topol.msize,'coord',som_unit_coords(map));
Daniel@0 281 %
Daniel@0 282 % OBJECT TAGS
Daniel@0 283 %
Daniel@0 284 % No tags are set.
Daniel@0 285 %
Daniel@0 286 % SEE ALSO
Daniel@0 287 %
Daniel@0 288 % som_show The basic map visualization routine
Daniel@0 289 % som_cplane The basic component plane visualization
Daniel@0 290 % som_connection The basic topological connections
Daniel@0 291 % scatter Scatter plots
Daniel@0 292 % scatter3 3-dimensional scatter plots
Daniel@0 293
Daniel@0 294 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
Daniel@0 295 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 296
Daniel@0 297 % Version 2.0beta Johan 061099 juuso 151199 310300
Daniel@0 298
Daniel@0 299 %% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 300
Daniel@0 301 True=1; False=0; % const.
Daniel@0 302 m=[]; l=[]; t=[]; s=[]; % default values for outputs
Daniel@0 303 Ref=som_set('som_grid'); % reference struct
Daniel@0 304
Daniel@0 305 num_of_args=length(varargin); % numb. of varargins
Daniel@0 306
Daniel@0 307 if num_of_args==0,
Daniel@0 308 S=som_set('som_grid');
Daniel@0 309 return;
Daniel@0 310 end
Daniel@0 311
Daniel@0 312 switch class(varargin{1})
Daniel@0 313 case 'struct'
Daniel@0 314 S=varargin{1};
Daniel@0 315 first_identifier=2;
Daniel@0 316 if ~isfield(S,'type'),
Daniel@0 317 error('Input struct is invalid: field ''type'' is missing.');
Daniel@0 318 end
Daniel@0 319 switch S.type
Daniel@0 320 case 'som_grid'
Daniel@0 321 S=varargin{1};
Daniel@0 322 first_identifier=2;
Daniel@0 323 case 'som_map'
Daniel@0 324 Ref.lattice=S.topol.lattice;
Daniel@0 325 Ref.msize=S.topol.msize;
Daniel@0 326 Ref.shape=S.topol.shape;
Daniel@0 327 S=Ref;
Daniel@0 328 first_identifier=2;
Daniel@0 329 case 'som_topol'
Daniel@0 330 Ref.lattice=S.lattice;
Daniel@0 331 Ref.msize=S.msize;
Daniel@0 332 Ref.shape=S.shape;
Daniel@0 333 S=Ref;
Daniel@0 334 first_identifier=2;
Daniel@0 335 otherwise
Daniel@0 336 error('Input struct has to be of type som_grid, som_map or som_topol.');
Daniel@0 337 end
Daniel@0 338 case 'cell'
Daniel@0 339 S=varargin{1};
Daniel@0 340 first_identifier=2;
Daniel@0 341 if vis_valuetype(S,{'topol_cell_no_shape'}),
Daniel@0 342 Ref.lattice=S{1};
Daniel@0 343 Ref.msize=S{2};
Daniel@0 344 elseif vis_valuetype(S,{'topol_cell'}),
Daniel@0 345 Ref.lattice=S{1};
Daniel@0 346 Ref.msize=S{2};
Daniel@0 347 Ref.shape=S{3};
Daniel@0 348 else
Daniel@0 349 error(['The cell value for 1st argument has to be {lattice, msize}' ...
Daniel@0 350 'or {lattice, msize, shape}.']);
Daniel@0 351 end
Daniel@0 352 S=Ref;
Daniel@0 353 case{'double','sparse','char'}
Daniel@0 354 % Set defaults
Daniel@0 355 S=Ref;
Daniel@0 356 first_identifier=3;
Daniel@0 357 if num_of_args<2,
Daniel@0 358 error('Not enough input arguments.');
Daniel@0 359 end
Daniel@0 360 S.lattice=varargin{1};
Daniel@0 361 S.msize=varargin{2};
Daniel@0 362 otherwise
Daniel@0 363 error('Invalid input arguments!');
Daniel@0 364 end
Daniel@0 365
Daniel@0 366 %% Check input args %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 367
Daniel@0 368 for i=first_identifier:2:num_of_args,
Daniel@0 369 if ischar(varargin{i}) & isfield(Ref,lower(varargin{i})),
Daniel@0 370 if i+1>num_of_args,
Daniel@0 371 error('Invalid identifier-value pairs or wrong argument order.');
Daniel@0 372 else
Daniel@0 373 S=setfield(S,lower(varargin{i}),varargin{i+1});
Daniel@0 374 end
Daniel@0 375 elseif ischar(varargin{i}),
Daniel@0 376 error(['Identifier ''' varargin{i} ''' is unknown.']);
Daniel@0 377 else
Daniel@0 378 error('Invalid identifier-value pairs or wrong argument order.');
Daniel@0 379 end
Daniel@0 380 end
Daniel@0 381
Daniel@0 382 % msize
Daniel@0 383
Daniel@0 384 if ~vis_valuetype(S.msize,{'1x2'}),
Daniel@0 385 error('msize has to be a 1x2 vector.');
Daniel@0 386 end
Daniel@0 387 munits=prod(S.msize);
Daniel@0 388
Daniel@0 389 % Default coordinates according to negihborhood
Daniel@0 390
Daniel@0 391 if isempty(S.coord),
Daniel@0 392 if ischar(S.lattice),
Daniel@0 393 switch S.lattice,
Daniel@0 394 case{'hexa','rect'}
Daniel@0 395 S.coord=som_vis_coords(S.lattice,S.msize);
Daniel@0 396 otherwise
Daniel@0 397 error('String value for lattice must be ''hexa'' or ''rect''.');
Daniel@0 398 end
Daniel@0 399 else
Daniel@0 400 error('Lattice is not ''hexa'' or ''rect'': coordinates must be given.');
Daniel@0 401 end
Daniel@0 402 end
Daniel@0 403
Daniel@0 404 % connections
Daniel@0 405
Daniel@0 406 type=class(S.lattice);
Daniel@0 407 switch type
Daniel@0 408 case {'sparse','double'} % free topology
Daniel@0 409 fixedline=False;
Daniel@0 410 case 'char' % default topologies (hexa,char)
Daniel@0 411 switch S.lattice
Daniel@0 412 case 'hexa'
Daniel@0 413 hexa=True;
Daniel@0 414 case 'rect'
Daniel@0 415 hexa=False;
Daniel@0 416 otherwise
Daniel@0 417 error('Unknown lattice or neighborhood.');
Daniel@0 418 end
Daniel@0 419
Daniel@0 420 % If topology is hexa/rect but linetype, color etc. is
Daniel@0 421 % not constant, the topology is set to free
Daniel@0 422
Daniel@0 423 if size(S.linewidth,1)>1 | size(S.linecolor,1)>1 | ...
Daniel@0 424 iscell(S.linecolor) % matrix or cell = not constant
Daniel@0 425 fixedline=False;
Daniel@0 426 S.lattice=som_connection({S.lattice,S.msize,S.shape});
Daniel@0 427 else
Daniel@0 428 fixedline=True;
Daniel@0 429 end
Daniel@0 430 end
Daniel@0 431
Daniel@0 432 % Check coordinate matrix size and set dummy zeros to z-axis
Daniel@0 433 % if 2D coordinates (always 3D plots!)
Daniel@0 434
Daniel@0 435 if ~vis_valuetype(S.coord,{[munits 2],[munits 3]}),
Daniel@0 436 error('Coordinate matrix has wrong size.');
Daniel@0 437 elseif size(S.coord,2)==2,
Daniel@0 438 S.coord(:,3)=0;
Daniel@0 439 end
Daniel@0 440
Daniel@0 441 % Fixed marker size, color, type?
Daniel@0 442
Daniel@0 443 if size(S.markersize,1)>1 | size(S.markercolor,1)>1 | size(S.marker,1)>1
Daniel@0 444 fixedmarker=False;
Daniel@0 445 else
Daniel@0 446 fixedmarker=True;
Daniel@0 447 end
Daniel@0 448
Daniel@0 449 % Check labels
Daniel@0 450
Daniel@0 451 if ~vis_valuetype(S.label,{'chararray','2Dcellarray_of_char'}) ...
Daniel@0 452 & ~isempty(S.label),
Daniel@0 453 error('Labels should be in a char array or cell array of strings.');
Daniel@0 454 elseif ischar(S.label)
Daniel@0 455 S.label=cellstr(S.label);
Daniel@0 456 end
Daniel@0 457
Daniel@0 458 if size(S.label,1) ~= munits & ~isempty(S.label),
Daniel@0 459 error('Number of labels and map size do not match.');
Daniel@0 460 end
Daniel@0 461
Daniel@0 462 % Check line width, marker size, marker color,
Daniel@0 463 % label size label color and surf sizes&types:
Daniel@0 464
Daniel@0 465 if ~vis_valuetype(S.linewidth,{[munits munits] [1 1]}),
Daniel@0 466 error('LineWidth matrix value has wrong size or dimension.');
Daniel@0 467 elseif any(S.linewidth(:)<0),
Daniel@0 468 error('All elements of LineWidth must be non-negative.');
Daniel@0 469 elseif ~vis_valuetype(S.markersize,{[munits 1] [1 1]}),
Daniel@0 470 error('MarkerSize matrix value has wrong size or dimension.');
Daniel@0 471 elseif any(S.markersize(:)<0),
Daniel@0 472 error('All elements of MarkerSize must be non-negative.');
Daniel@0 473 elseif ~vis_valuetype(S.markercolor,{'1x3rgb','colorstyle'}) & ...
Daniel@0 474 ~vis_valuetype(S.markercolor,{[munits 3],'nx3rgb'},'all'),
Daniel@0 475 error('MarkerColor should be a ColorSpec or Mx3 matrix of RGB triples.');
Daniel@0 476 elseif ~vis_valuetype(S.labelcolor,{'1x3rgb','colorstyle','xor'}),
Daniel@0 477 error('LabelColor shoud be a ColorSpec or ''xor'' or ''none''.')
Daniel@0 478 elseif ~vis_valuetype(S.labelsize,{'1x1'})
Daniel@0 479 error('LabelSize should be a scalar.');
Daniel@0 480 elseif ~isempty(S.surf) & ~vis_valuetype(S.surf,{[munits 1] [munits 3]});
Daniel@0 481 error('Surf matrix value has wrong size or dimension.');
Daniel@0 482 end
Daniel@0 483
Daniel@0 484 % Check marker type & size
Daniel@0 485
Daniel@0 486 if vis_valuetype(S.marker,{'cellcolumn_of_char'})
Daniel@0 487 % Don't bother to check the mareker strings in this case
Daniel@0 488 % let the plot3 handle them; it returns quite understandable
Daniel@0 489 % error messages, anyway
Daniel@0 490
Daniel@0 491 if ~size(S.marker) == [munits 1],
Daniel@0 492 error(['Marker should be one of Matlab''s valid marker type,' ...
Daniel@0 493 ' string ''none'' or a Mx1 cell array of these.']);
Daniel@0 494 end
Daniel@0 495 elseif ~vis_valuetype(S.marker,{'markerstyle','none'}),
Daniel@0 496 error(['Marker should be one of Matlab''s valid marker type,' ...
Daniel@0 497 ' string ''none'' or a Mx1 cell array of these.']);
Daniel@0 498 end
Daniel@0 499
Daniel@0 500 % Check line type & size: only one line style allowed
Daniel@0 501
Daniel@0 502 if ~vis_valuetype(S.line,{'linestyle','none'})
Daniel@0 503 error(['Line should be a valid Matlab''s line style string or' ...
Daniel@0 504 ' string ''none''.']);
Daniel@0 505 end
Daniel@0 506
Daniel@0 507 % Check line color
Daniel@0 508
Daniel@0 509 if iscell(S.linecolor),
Daniel@0 510 if ndims(S.linecolor) ~= 2 | any(size(S.linecolor) ~= [1 3]),
Daniel@0 511 error('Cell input for LineColor should be of form {r,g,b}.')
Daniel@0 512 elseif ~vis_valuetype(S.linecolor{1},{[munits munits],'nxn[0,1]'},'all')| ...
Daniel@0 513 ~vis_valuetype(S.linecolor{2},{[munits munits],'nxn[0,1]'},'all')| ...
Daniel@0 514 ~vis_valuetype(S.linecolor{3},{[munits munits],'nxn[0,1]'},'all'),
Daniel@0 515 error(['In cell input {r,g,b} some matrix r,g or b is invalid: ' ...
Daniel@0 516 'Size must be MxM and values in interval [0,1].']);
Daniel@0 517 end
Daniel@0 518 elseif ~vis_valuetype(S.linecolor,{'colorstyle','1x3rgb'}) & ...
Daniel@0 519 ~vis_valuetype(S.linecolor,{'nxnx3rgb', [munits munits 3]},'all'),
Daniel@0 520 error('Invalid LineColor: see help text for valid values.'),
Daniel@0 521 elseif vis_valuetype(S.linecolor, {'none'}),
Daniel@0 522 error('LineColor ''none'' not allowed: set Line to ''none'' instead.');
Daniel@0 523 end
Daniel@0 524
Daniel@0 525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 526 %% Action
Daniel@0 527
Daniel@0 528 memhold=ishold; % take hold state
Daniel@0 529 if ~memhold
Daniel@0 530 cla;
Daniel@0 531 end
Daniel@0 532 hold on;
Daniel@0 533
Daniel@0 534 % Set surf if it exist
Daniel@0 535
Daniel@0 536 if ~isempty(S.surf),
Daniel@0 537 for i=1:3,
Daniel@0 538 s(:,:,i)=reshape(S.coord(:,i),S.msize);
Daniel@0 539 end
Daniel@0 540 s(:,:,4:3+size(S.surf,2))=reshape(S.surf,[S.msize size(S.surf,2)]);
Daniel@0 541 s=surf(s(:,:,1),s(:,:,2),s(:,:,3),s(:,:,4:end));
Daniel@0 542 set(s,'EdgeColor','none','Marker','none','FaceColor','interp');
Daniel@0 543 end
Daniel@0 544
Daniel@0 545
Daniel@0 546 if fixedline,
Daniel@0 547 % Line properties are fixed: draw fast, but
Daniel@0 548 % if line is set to 'none' set empty handle ans skip
Daniel@0 549 if strcmp(S.line,'none')
Daniel@0 550 l={};
Daniel@0 551 else
Daniel@0 552 p1=reshape(S.coord, [S.msize 3]);
Daniel@0 553 p2=zeros(size(p1)-[0 1 0]);
Daniel@0 554 p2(1:2:end,:,:)=p1(1:2:end,2:end,:);
Daniel@0 555 p2(2:2:end,:,:)=p1(2:2:end,1:end-1,:);
Daniel@0 556
Daniel@0 557 l{1}=plot3(p1(:,:,1), p1(:,:,2), p1(:,:,3), ...
Daniel@0 558 'Color', S.linecolor(1,:), ...
Daniel@0 559 'LineWidth', S.linewidth(1), ...
Daniel@0 560 'LineStyle', S.line);
Daniel@0 561 l{2}=plot3(p1(:,:,1)', p1(:,:,2)', p1(:,:,3)', ...
Daniel@0 562 'Color', S.linecolor(1,:), ...
Daniel@0 563 'LineWidth', S.linewidth(1), ...
Daniel@0 564 'LineStyle', S.line);
Daniel@0 565 if hexa,
Daniel@0 566 l{3}=plot3(p2(:,:,1), p2(:,:,2), p2(:,:,3), ...
Daniel@0 567 'Color', S.linecolor(1,:), ...
Daniel@0 568 'LineWidth', S.linewidth(1), ...
Daniel@0 569 'LineStyle', S.line);
Daniel@0 570 end
Daniel@0 571 end
Daniel@0 572 l=cat(1,l{:});
Daniel@0 573 else
Daniel@0 574 % Variable properties: draw connection by connection
Daniel@0 575
Daniel@0 576 [I,J,lw]=find(S.lattice);
Daniel@0 577 x=[S.coord(I,1)'; S.coord(J,1)'];
Daniel@0 578 y=[S.coord(I,2)'; S.coord(J,2)'];
Daniel@0 579 z=[S.coord(I,3)'; S.coord(J,3)'];
Daniel@0 580 if S.linewidth(1)==0,
Daniel@0 581 linewidth=0.5;
Daniel@0 582 else
Daniel@0 583 linewidth=S.linewidth(1);
Daniel@0 584 end
Daniel@0 585 if ndims(S.linecolor) ~= 3
Daniel@0 586 if isstr(S.linecolor)
Daniel@0 587 l=plot3(x, y, z, ...
Daniel@0 588 'Color', S.linecolor, ...
Daniel@0 589 'LineWidth', linewidth, ...
Daniel@0 590 'LineStyle',S.line);
Daniel@0 591 else
Daniel@0 592 if iscell(S.linecolor)
Daniel@0 593 lcolor=[S.linecolor{1}(1,1) S.linecolor{2}(1,1) S.linecolor{3}(1,1)];
Daniel@0 594 l=plot3(x, y, z, ...
Daniel@0 595 'Color', lcolor, ...
Daniel@0 596 'LineWidth', linewidth, ...
Daniel@0 597 'LineStyle',S.line);
Daniel@0 598 else
Daniel@0 599 l=plot3(x, y, z, ...
Daniel@0 600 'Color', S.linecolor(1,:), ...
Daniel@0 601 'LineWidth', linewidth, ...
Daniel@0 602 'LineStyle',S.line);
Daniel@0 603 end
Daniel@0 604 end
Daniel@0 605 else
Daniel@0 606 l=plot3(x, y, z, ...
Daniel@0 607 'Color', S.linecolor(1,1,:), ...
Daniel@0 608 'LineWidth', linewidth, ...
Daniel@0 609 'LineStyle',S.line);
Daniel@0 610 end
Daniel@0 611 end
Daniel@0 612
Daniel@0 613 if fixedmarker,
Daniel@0 614
Daniel@0 615 % If marker is set to 'none' skip and set empty handle
Daniel@0 616 if strcmp(S.marker,'none')
Daniel@0 617 m=[];
Daniel@0 618 else
Daniel@0 619 % Fixed markers: draw all in one command
Daniel@0 620
Daniel@0 621 m=plot3(S.coord(:,1), S.coord(:,2), S.coord(:,3), ...
Daniel@0 622 'LineStyle', 'none', ...
Daniel@0 623 'Marker', S.marker, ...
Daniel@0 624 'MarkerSize', S.markersize(1), ...
Daniel@0 625 'MarkerFaceColor', S.markercolor(1,:), ...
Daniel@0 626 'MarkerEdgeColor', S.markercolor(1,:));
Daniel@0 627 end
Daniel@0 628 else
Daniel@0 629 % Variable marker properties: draw marker by marker
Daniel@0 630
Daniel@0 631 x=[S.coord(:,1)'; S.coord(:,1)'];
Daniel@0 632 y=[S.coord(:,2)'; S.coord(:,2)'];
Daniel@0 633 z=[S.coord(:,3)'; S.coord(:,3)'];
Daniel@0 634 if iscell(S.marker)
Daniel@0 635 marker=S.marker{1};
Daniel@0 636 else
Daniel@0 637 marker=S.marker(1);
Daniel@0 638 end
Daniel@0 639 sz=max(S.markersize(1),0.1);
Daniel@0 640 m=plot3(x, y, z, ...
Daniel@0 641 'LineStyle', 'none', ...
Daniel@0 642 'Marker', marker, ...
Daniel@0 643 'MarkerSize', sz, ...
Daniel@0 644 'MarkerFaceColor', S.markercolor(1,:), ...
Daniel@0 645 'MarkerEdgeColor', S.markercolor(1,:));
Daniel@0 646 end
Daniel@0 647
Daniel@0 648 L=length(l);
Daniel@0 649 n=munits;
Daniel@0 650
Daniel@0 651 %%% Set variable properties %%%
Daniel@0 652
Daniel@0 653 % Line width
Daniel@0 654
Daniel@0 655 if length(S.linewidth)>1
Daniel@0 656 lwidth=diag(S.linewidth(I,J));
Daniel@0 657
Daniel@0 658 % Handle zero width
Daniel@0 659 iszero=(lwidth == 0);lwidth(iszero)=0.5;
Daniel@0 660 for i=1:length(l),
Daniel@0 661 set(l(i),'LineWidth', lwidth(i));
Daniel@0 662 end
Daniel@0 663 if ~isempty(iszero), % zero width
Daniel@0 664 set(l(iszero),'Visible','off');
Daniel@0 665 end
Daniel@0 666 end
Daniel@0 667
Daniel@0 668 % Line color
Daniel@0 669
Daniel@0 670 if size(S.linecolor,1)>1 | iscell(S.linecolor)
Daniel@0 671 if length(size(S.linecolor)) == 3 | iscell(S.linecolor)
Daniel@0 672 if ~iscell(S.linecolor)
Daniel@0 673 for i=1:L
Daniel@0 674 set(l(i),'Color',S.linecolor(I(i),J(i),:));
Daniel@0 675 end
Daniel@0 676 else
Daniel@0 677 for i=1:L
Daniel@0 678 lcolor=[S.linecolor{1}(I(i),J(i)),...
Daniel@0 679 S.linecolor{2}(I(i),J(i)),...
Daniel@0 680 S.linecolor{3}(I(i),J(i))];
Daniel@0 681 set(l(i),'Color',lcolor);
Daniel@0 682 end
Daniel@0 683 end
Daniel@0 684 else
Daniel@0 685 for i=1:L,
Daniel@0 686 set(l(i),'Color', S.linecolor(I(i),:));
Daniel@0 687 end
Daniel@0 688 end
Daniel@0 689 end
Daniel@0 690
Daniel@0 691 % Marker size
Daniel@0 692
Daniel@0 693 if length(S.markersize)>1
Daniel@0 694 % handle zero size
Daniel@0 695 iszero=find(~S.markersize);
Daniel@0 696 S.markersize(iszero)=1;
Daniel@0 697 for i=1:n,
Daniel@0 698 set(m(i),'MarkerSize', S.markersize(i));
Daniel@0 699 end
Daniel@0 700 if ~isempty(iszero), % zero size
Daniel@0 701 set(m(iszero),'Visible','off');
Daniel@0 702 end
Daniel@0 703 end
Daniel@0 704
Daniel@0 705 % Marker type
Daniel@0 706
Daniel@0 707 if size(S.marker,1)>1
Daniel@0 708 S.marker=char(S.marker);
Daniel@0 709 for i=1:n,
Daniel@0 710 set(m(i),'Marker', S.marker(i));
Daniel@0 711 end
Daniel@0 712 end
Daniel@0 713
Daniel@0 714 % Marker color
Daniel@0 715
Daniel@0 716 if size(S.markercolor,1)>1
Daniel@0 717 for i=1:n,
Daniel@0 718 set(m(i),'MarkerFaceColor', S.markercolor(i,:), ...
Daniel@0 719 'MarkerEdgeColor', S.markercolor(i,:));
Daniel@0 720 end
Daniel@0 721 end
Daniel@0 722
Daniel@0 723 % Set labels if they exist
Daniel@0 724
Daniel@0 725 if ~isempty(S.label)
Daniel@0 726 if vis_valuetype(S.labelcolor,{'xor'}),
Daniel@0 727 S.labelcolor='g';
Daniel@0 728 XOR=1;
Daniel@0 729 else
Daniel@0 730 XOR=0;
Daniel@0 731 end
Daniel@0 732 if vis_valuetype(S.labelcolor,{'none'}),
Daniel@0 733 S.labelcolor='g';
Daniel@0 734 VIS = 1;
Daniel@0 735 else
Daniel@0 736 VIS = 0;
Daniel@0 737 end
Daniel@0 738 for i=1:size(S.label,1),
Daniel@0 739 L=cat(1,S.label(i,:));
Daniel@0 740 for j=length(L):-1:1,
Daniel@0 741 if isempty(L{j}),
Daniel@0 742 L=L(1:end-1);
Daniel@0 743 end
Daniel@0 744 end
Daniel@0 745
Daniel@0 746 if isempty(L),
Daniel@0 747 L='';
Daniel@0 748 end
Daniel@0 749 t(i)=text(S.coord(i,1), S.coord(i,2), S.coord(i,3), L,...
Daniel@0 750 'FontSize', S.labelsize, 'Color',S.labelcolor, ...
Daniel@0 751 'HorizontalAlignment', 'center');
Daniel@0 752 end
Daniel@0 753 if XOR
Daniel@0 754 set(t,'EraseMode','xor');
Daniel@0 755 end
Daniel@0 756 if VIS
Daniel@0 757 set(t,'Visible','off');
Daniel@0 758 end
Daniel@0 759 else
Daniel@0 760 t=[];
Daniel@0 761 end
Daniel@0 762
Daniel@0 763 %% Set hold state
Daniel@0 764
Daniel@0 765 if ~memhold,
Daniel@0 766 hold off;
Daniel@0 767 end
Daniel@0 768
Daniel@0 769 if nargout==0,
Daniel@0 770 clear S m l t s;
Daniel@0 771 end