wolffd@0: function h=som_recolorbar(p, ticks, scale, labels) wolffd@0: wolffd@0: %SOM_RECOLORBAR Refresh and rescale colorbars in the current SOM_SHOW fig. wolffd@0: % wolffd@0: % h = som_recolorbar([p], [ticks], [scaling], [labels]) wolffd@0: % wolffd@0: % colormap(jet); som_recolorbar wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional) wolffd@0: % [p] (vector) subplot number vector wolffd@0: % (string) 'all' (the default), 'comp' to process only wolffd@0: % component planes wolffd@0: % [ticks] (string) 'auto' or 'border', default: 'auto' wolffd@0: % (cell array) p x 1 cell array of p row vectors wolffd@0: % (vector) the same ticks are applied to all given subplots wolffd@0: % (scalar) value is at least 2: the number of ticks to show, wolffd@0: % evenly spaced between and including minimum and maximum wolffd@0: % [scale] (string) 'denormalized' or 'normalized' (the default) wolffd@0: % [labels] (cell array) p x 1 cell array of cells containing strings wolffd@0: % wolffd@0: % h (vector) handles to the colorbar objects. wolffd@0: % wolffd@0: % This function refreshes the colorbars in the figure created by SOM_SHOW. wolffd@0: % Refreshing is necessary if you have changed the colormap. wolffd@0: % Each colorbar has letter 'd' or 'n' and possibly 'u' as label. Letter 'd' means wolffd@0: % that the scale is denormalized, letter 'n' that the scale is wolffd@0: % normalized, and 'u' is for user specified labels. wolffd@0: % wolffd@0: % For more help, try 'type som_recolorbar' or check out online documentation. wolffd@0: % See also SOM_SHOW wolffd@0: wolffd@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % wolffd@0: % som_recolorbar wolffd@0: % wolffd@0: % PURPOSE wolffd@0: % wolffd@0: % Refreshes the the colorbars in the figure. wolffd@0: % wolffd@0: % SYNTAX wolffd@0: % wolffd@0: % h = som_recolorbar wolffd@0: % h = som_recolorbar(p) wolffd@0: % h = som_recolorbar(p, ticks) wolffd@0: % h = som_recolorbar(p, ticks, scaling) wolffd@0: % h = som_recolorbar(p, ticks, scaling, labels) wolffd@0: % wolffd@0: % DESCRIPTION wolffd@0: % wolffd@0: % This function refreshes the colorbars in the figure created by SOM_SHOW. wolffd@0: % Refreshing is necessary if you have changed the colormap. Each colorbar wolffd@0: % has letter 'd' or 'n' and possibly 'u' as label. Letter 'd' means that the wolffd@0: % scale is denormalized, letter 'n' that the scale is normalized, and 'u' is wolffd@0: % for user specified labels. wolffd@0: % wolffd@0: % Different argument combinations: wolffd@0: % wolffd@0: % 1. Argument 'ticks' has string values: wolffd@0: % - 'auto' for input argument ticks sets the automatic tick wolffd@0: % marking on (factory default). wolffd@0: % - 'border' sets the tick marks to the color borders. This is wolffd@0: % convenient if there are only few colors in use. wolffd@0: % wolffd@0: % Argument scale controls the scaling of the tick mark label values. wolffd@0: % 'normalized' means that the tick mark labels are directly the values wolffd@0: % of the ticks, that is, they refer to the map codebook values. wolffd@0: % Value 'denormalized' scales the tick mark label values back to the original wolffd@0: % data scaling. This is made using som_denormalize_data. wolffd@0: % wolffd@0: % 2. Argument 'ticks' is a cell array of vectors: wolffd@0: % The values are set to be the tick marks to the colorbar specified by p. wolffd@0: % - if arg. scale is 'normalized' the ticks are set directly to the colorbar. wolffd@0: % - if arg. scale is 'denormalized' the tick values are first normalized wolffd@0: % in the same way as the data. wolffd@0: % wolffd@0: % 3. Argument 'ticks' is a vector wolffd@0: % As above, but the same values are used for all (given) subplots. wolffd@0: % wolffd@0: % 4. Argument 'ticks' is a scalar wolffd@0: % The ticks are set to equally spaced values between (and including) wolffd@0: % minimum and maximum. wolffd@0: % wolffd@0: % Argument 'labels' specify user defined labels to the tick marks wolffd@0: % wolffd@0: % NOTE: ticks are rounded to contain three significant digits. wolffd@0: % wolffd@0: % OPTIONAL INPUT ARGUMENTS wolffd@0: % wolffd@0: % p (vector) subplot number vector wolffd@0: % (string) 'all' (the default), 'comp' to effect only wolffd@0: % component planes wolffd@0: % wolffd@0: % ticks (string) 'auto' or 'border', default: 'auto' wolffd@0: % (cell array) p x 1 cell array of p row vectors wolffd@0: % (vector) as the cell array, but the same vector is wolffd@0: % applied to all given subplots wolffd@0: % (scalar) the number of ticks to show: these are wolffd@0: % evenly space between minimum and maximum wolffd@0: % wolffd@0: % scale (string) 'denormalized' or 'normalized' (the default) wolffd@0: % wolffd@0: % labels (cell array) p x 1 cell array of cells containing strings wolffd@0: % wolffd@0: % OUTPUT ARGUMENTS wolffd@0: % wolffd@0: % h (vector) handles to the colorbar objects. wolffd@0: % wolffd@0: % EXAMPLE wolffd@0: % wolffd@0: % colormap(jet(5)); som_recolorbar('all','border','denormalized') wolffd@0: % % Uses five colors and sets the ticks on the color borders. wolffd@0: % % Tick label values are denormalized back to the original data scaling wolffd@0: % wolffd@0: % colormap(copper(64));som_recolorbar wolffd@0: % % changes to colormap copper and resets default ticking and labeling wolffd@0: % wolffd@0: % som_recolorbar('all',3) wolffd@0: % % To put 3 ticks to each colorbar so that minimum, mean and wolffd@0: % % maximum values on the colorbar are shown. wolffd@0: % wolffd@0: % som_recolorbar([1 3],{[0.1 0.2 0.3];[0.2 0.4]},'denormalized') wolffd@0: % % Ticks colorbar 1 by first normalizing values 0.1, 0.2, 0.3 and wolffd@0: % % then setting the ticks to the colorbar. Labels are of course wolffd@0: % % 0.1, 0.2 and 0.3. Ticks colorbar 3 in the same way using values wolffd@0: % % 0.2 and 0.4. wolffd@0: % wolffd@0: % som_recolorbar([2 4],{[0.1 0.2];[-1.2 3]},'normalized',{{'1' '2'};{'a' 'b'}}) wolffd@0: % % Ticks colorbar 2 and 4 directly to the specified values. Sets labels wolffd@0: % % '1' '2' and 'a' 'b' to the ticks. wolffd@0: % wolffd@0: % som_recolorbar([2 4],{[0.1 0.2];[-1.2 3]},'normalized',{{'1' '2'};{'a' 'b'}}) wolffd@0: % % as previous one, but normalizes tick values first wolffd@0: % wolffd@0: % SEE ALSO wolffd@0: % wolffd@0: % som_show Basic SOM visualization. wolffd@0: % som_normalize Normalization operations. wolffd@0: % som_denormalize Denormalization operations. wolffd@0: wolffd@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 1.0beta Johan 061197 wolffd@0: % Version 2.0beta juuso 151199 130300 160600 181101 wolffd@0: wolffd@0: %% Init & check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: error(nargchk(0, 4, nargin)) % check no. of input args wolffd@0: wolffd@0: % Check the subplot vector p and get the handles, exit if error wolffd@0: % Default subplot vector is 'all' wolffd@0: wolffd@0: if nargin < 1 | isempty(p) % default p wolffd@0: p= 'all'; wolffd@0: end wolffd@0: wolffd@0: % check SOM_SHOW and get the figure data. Exit, if error wolffd@0: wolffd@0: [handles, msg, lattice, msize, dim, normalization, comps]= ... wolffd@0: vis_som_show_data(p, gcf); wolffd@0: error(msg); wolffd@0: wolffd@0: if nargin < 2 | isempty(ticks) % default tick mode is 'auto' wolffd@0: ticks = 'auto'; wolffd@0: elseif isa(ticks,'cell') % check for cell wolffd@0: tickValues = ticks; wolffd@0: ticks= 'explicit'; wolffd@0: elseif isa(ticks,'double') & length(ticks)>1, wolffd@0: tickValues = {ticks}; wolffd@0: ticks = 'explicit'; wolffd@0: elseif isa(ticks,'double') & length(ticks)==1, wolffd@0: tickValues = max(2,round(ticks)); wolffd@0: ticks = 'evenspace'; wolffd@0: end wolffd@0: if ~ischar(ticks) % invalid argument wolffd@0: error('The second argument should be a string or a cell array of vectors.'); wolffd@0: end wolffd@0: wolffd@0: switch ticks % check ticks wolffd@0: case {'auto','border'}, % nill wolffd@0: case 'evenspace', wolffd@0: tickValues_tmp = cell(length(handles),1); wolffd@0: for i=1:length(handles), tickValues_tmp{i} = tickValues; end wolffd@0: tickValues = tickValues_tmp; wolffd@0: case 'explicit', wolffd@0: if length(tickValues)==1 & length(handles)>1, wolffd@0: tickValues_tmp = cell(length(handles),1); wolffd@0: for i=1:length(handles), tickValues_tmp{i} = tickValues{1}; end wolffd@0: tickValues = tickValues_tmp; wolffd@0: end wolffd@0: if length(tickValues) ~= length(handles), wolffd@0: error('Cell containing the ticks has wrong size.') wolffd@0: end wolffd@0: otherwise wolffd@0: error('''auto'' or ''border'' expected for the second argument.'); wolffd@0: end wolffd@0: wolffd@0: if nargin < 3 | isempty(scale) % default mode is normalized wolffd@0: scale= 'normalized'; wolffd@0: end wolffd@0: if ~ischar(scale) % check scale type wolffd@0: error('The third argument should be a string.'); wolffd@0: end wolffd@0: switch scale % check the string wolffd@0: case { 'normalized', 'denormalized'} % ok wolffd@0: case 'n', scale = 'normalized'; wolffd@0: case 'd', scale = 'denormalized'; wolffd@0: otherwise wolffd@0: error('''normalized'' or ''denormalized'' expected for the third argument.') wolffd@0: end wolffd@0: wolffd@0: if nargin < 4 | isempty(labels) % default is autolabeling wolffd@0: labels = 'auto'; wolffd@0: elseif ~isa(labels,'cell') % check type wolffd@0: error('The fourth argument should be a cell array of cells containing strings.') wolffd@0: else wolffd@0: labelValues=labels; % set labels wolffd@0: labels = 'explicit'; wolffd@0: if length(labelValues) == length(handles) % check size wolffd@0: ; wolffd@0: else wolffd@0: error('Cell containing the labels has wrong size') wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: n = size(colormap,1)+1; % number of colors+1 wolffd@0: h_ = zeros(length(handles),1); wolffd@0: wolffd@0: for i=1:length(handles), % MAIN LOOP BEGINS wolffd@0: axes(handles(i)); % set axes, refres colorbar and wolffd@0: if comps(i)>=0, wolffd@0: h_(i)=colorbar; % get colorbar handles wolffd@0: wolffd@0: colorbardir=get(h_(i),'YaxisLocation'); wolffd@0: switch colorbardir % get colorbar direction & wolffd@0: case 'left' % set some strings wolffd@0: Tick='Xtick'; Lim='Xlim'; LabelMode='XTickLabelMode'; Label='XtickLabel'; wolffd@0: case 'right' wolffd@0: Tick='Ytick'; Lim='Ylim'; LabelMode='YTickLabelMode'; Label='YtickLabel'; wolffd@0: otherwise wolffd@0: error('Internal error: unknown value for YaxisLocation'); % fatal wolffd@0: end wolffd@0: wolffd@0: switch ticks wolffd@0: case 'auto' wolffd@0: set(h_(i),LabelMode,'auto'); % factory default ticking wolffd@0: tickValues{i}=get(h_(i),Tick); % get tick values wolffd@0: case 'border' wolffd@0: limit=caxis; wolffd@0: t=linspace(limit(1),limit(2),n); % set n ticks between min and max wolffd@0: t([1 length(t)])=get(h_(i),Lim); % <- caxis is not necerraily the same wolffd@0: tickValues{i}=t; % as the colorbar min & max values wolffd@0: case 'evenspace' wolffd@0: limit = caxis; wolffd@0: t = linspace(limit(1),limit(2),tickValues{i}); wolffd@0: t([1 length(t)])=get(h_(i),Lim); wolffd@0: tickValues{i}=t; wolffd@0: case 'explicit' wolffd@0: if comps(i)>0, wolffd@0: if strcmp(scale,'normalized') % normalize tick values wolffd@0: tickValues{i} = som_normalize(tickValues{i},normalization{comps(i)}); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: otherwise wolffd@0: error('Internal error: unknown tick type') % this shouldn't happen wolffd@0: end wolffd@0: wolffd@0: %tickValues{i} = epsto0(tickValues{i}); wolffd@0: wolffd@0: switch labels wolffd@0: case 'auto' wolffd@0: switch scale wolffd@0: case 'normalized' wolffd@0: labelValues{i} = round2(tickValues{i}); % use the raw ones wolffd@0: case 'denormalized' % denormalize tick values wolffd@0: if comps(i)>0, wolffd@0: labelValues{i} = som_denormalize(tickValues{i},normalization{comps(i)}); wolffd@0: labelValues{i} = round2(labelValues{i}); % round the scale wolffd@0: else wolffd@0: labelValues{i} = round2(tickValues{i}); wolffd@0: end wolffd@0: otherwise wolffd@0: error('Internal error: unknown scale type'); % this shouldn't happen wolffd@0: end wolffd@0: case 'explicit' wolffd@0: ; % they are there already wolffd@0: otherwise wolffd@0: error('Internal error: unknown label type'); % this shouldn't happen wolffd@0: end wolffd@0: wolffd@0: set(h_(i),Tick,tickValues{i}); % set ticks and labels wolffd@0: set(h_(i),Label,labelValues{i}); wolffd@0: wolffd@0: if comps(i)>0, wolffd@0: % Label the colorbar with letter 'n' if normalized, with letter 'd' wolffd@0: % if denormalized and 'u' if the labels are user specified wolffd@0: mem_axes=gca; axes(h_(i)); wolffd@0: ch=' '; wolffd@0: if strcmp(scale,'normalized'), ch(1)='n'; end wolffd@0: if strcmp(scale,'denormalized'), ch(1)='d'; end wolffd@0: if strcmp(labels,'explicit'), ch(2)='u'; end wolffd@0: xlabel(ch); wolffd@0: axes(mem_axes); wolffd@0: end wolffd@0: end wolffd@0: end % MAIN LOOP ENDS wolffd@0: wolffd@0: wolffd@0: %% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: if nargout>0 wolffd@0: h=h_; wolffd@0: end wolffd@0: wolffd@0: return; wolffd@0: wolffd@0: %% Subfunction: ROUND2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % ROUND2 rounds the labels to tol significant digits wolffd@0: wolffd@0: function r=round2(d) wolffd@0: wolffd@0: tol=3; wolffd@0: wolffd@0: zero=(d==0); wolffd@0: d(zero)=1; wolffd@0: k=floor(log10(abs(d)))-(tol-1); wolffd@0: r=round(d./10.^k).*10.^k; wolffd@0: r(zero)=0; wolffd@0: %r=epsto0(r); wolffd@0: wolffd@0: %% Subfunction: ISVECTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: function t=isvector(v) wolffd@0: % ISVECTOR checks if a matrix is a vector or not wolffd@0: wolffd@0: t=(ndims(v) == 2 & min(size(v)) == 1) & isnumeric(v); wolffd@0: wolffd@0: %% Subfunction: EPSTO0 wolffd@0: wolffd@0: function t=epsto0(t) wolffd@0: % EPSTO0 checks whether first tick value is *very* close to zero, wolffd@0: % if so sets it to zero. wolffd@0: wolffd@0: if (t(end)-t(1))/t(end) > 1-0.005 & abs(t(1))<1, t(1) = 0; end wolffd@0: wolffd@0: wolffd@0: wolffd@0: