⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 heatmaptext.m

📁 Heat maps with numeric values overlayed
💻 M
字号:
function  heatmaptext(data,varargin)%HEATMAPTEXT creates a heatmap with the values shown as text.%%   HEATMAPTEXT(SEQ) counts the number of occurrences of each codon in the%   sequence and displays a formatted table of the result.%%   HEATMAPTEXT(...,'PRECISION',P) displays the data values with a maximum N%   digits of precision.  The default number of digits of precision is 2.%%   HEATMAPTEXT(SEQ, ... ,'FONTCOLOR',COL) sets the color of the text to%   COL, where COL is a valied Handle Graphics color property.%%   HEATMAPTEXT(SEQ, ... ,'COLORBAR',false) hides the colorbar.%%   Examples:%%       ih = gallery('invhess',10);%       heatmaptext(ih);%%       figure;%       m = gallery('moler',30);%       heatmaptext(m);%%       figure;%       heatmaptext(rand(20,10),'fontcolor','r','precision',4);%       colormap(bone);%%   See also COLORMAP, IMAGESC, TEXT.%   Copyright 2007 The MathWorks, Inc.%   $Revision: 1.1 $  $Date: 2007/08/08 21:56:22 $precision = 2;textColor = 'k';cBar = true;if nargin > 1    if rem(nargin,2)== 0        error('heatmaptext:IncorrectNumberOfArguments',...            'Incorrect number of arguments to %s.',mfilename);    end    okargs = {'precision','fontcolor','colorbar'};    for j=1:2:nargin-2        pname = varargin{j};        pval = varargin{j+1};        k = strmatch(lower(pname), okargs);        if isempty(k)            error('heatmaptext:UnknownParameterName',...                'Unknown parameter name: %s.',pname);        elseif length(k)>1            error('heatmaptext:AmbiguousParameterName',...                'Ambiguous parameter name: %s.',pname);        else            switch(k)                case 1  % frame                    precision = pval;                case 2  % direction                    textColor = pval;                case 3                    cBar = pval;            end        end    endendim = imagesc(data);imAxes =get(im,'parent');hFig = get(imAxes,'parent');fs = getBestFontSize(imAxes);    showText = true;if fs == 0    showText = false;    fs = 9;endaxis off;if cBar    colorbar;end[rows, cols] = size(data);if max(rows,cols) > 50    warning('This might be quite slow...');endtextHandles = zeros(size(data))';for i = 1:rows    for j = 1:cols                 textHandles(j,i) = text(j,i,num2str(data(i,j),precision),...                'color',textColor,'horizontalAlignment','center',...                'fontsize',fs,'clipping','on','visible','off');    endendif  showText     set(textHandles,'visible','on');endset(imAxes,'UserData',textHandles);localSetupPositionListener(hFig,imAxes);localSetupLimitListener(imAxes);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function fs = getBestFontSize(imAxes)% Try to keep font size reasonable for texthFig = get(imAxes,'Parent');magicNumber = 80;nrows = diff(get(imAxes,'YLim'));ncols = diff(get(imAxes,'XLim'));if ncols < magicNumber && nrows < magicNumber    ratio = max(get(hFig,'Position').*[0 0 0 1])/max(nrows,ncols);elseif ncols < magicNumber    ratio = max(get(hFig,'Position').*[0 0 0 1])/ncols;elseif nrows < magicNumber    ratio = max(get(hFig,'Position').*[0 0 0 1])/nrows;else    ratio = 1;endfs = min(9,ceil(ratio/4));    % the gold formulaif fs < 4    fs = 0;end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function localSetupPositionListener(hFig,imAxes)% helper function to sets up listeners for resizing, so we can detect if% we would need to change the fontsizehgp     = findpackage('hg');figC   = findclass(hgp,'figure');PostPositionListener = handle.listener(hFig,'ResizeEvent',...    {@localPostPositionListener,imAxes});% PostPositionListener = handle.listener(hFig,figC.findprop('Position'),...%     'PropertyPostSet',{@localPostPositionListener,imAxes});% listeners = getappdata(hFig,'HeatMapListeners');setappdata(hFig,'HeatMapListeners',[listeners PostPositionListener]);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function localPostPositionListener(hSrc,event,imAxes) %#oktextHandles = get(imAxes,'UserData');fs = getBestFontSize(imAxes);if fs > 0    set(textHandles,'fontsize',fs,'visible','on');else    set(textHandles,'visible','off');end% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function localSetupLimitListener(imAxes)% helper function to sets up listeners for zooming, so we can detect if% we would need to change the fontsizehgp     = findpackage('hg');axesC   = findclass(hgp,'axes');LimListener = handle.listener(imAxes,[axesC.findprop('XLim') axesC.findprop('YLim')],...    'PropertyPostSet',@localLimitListener);hFig = get(imAxes,'Parent');listeners = getappdata(hFig,'HeatMapListeners');setappdata(hFig,'HeatMapListeners',[listeners LimListener]);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function localLimitListener(hSrc,event)%#okimAxes = event.AffectedObject;textHandles = get(imAxes,'UserData');fs = getBestFontSize(imAxes);if fs > 0    set(textHandles,'fontsize',fs,'visible','on');else    set(textHandles,'visible','off');end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -