Home > atmlab > graphs > sanepcolor.m

sanepcolor

PURPOSE ^

SANEPCOLOR simple wrapper for pcolor

SYNOPSIS ^

function p = sanepcolor(varargin)

DESCRIPTION ^

SANEPCOLOR  simple wrapper for pcolor

 Unlike the built-in pcolor command, this function does not "cut off" the
 last row and column of the input matrix.  In this way, sanePColor is
 intended to be as easy to use as imagesc, but allows the user to specify
 the x and y coordinates of each cell if desired.  This function is also
 useful as an alternative means of generating images to print to PDF that
 are compatible with OS X's "Preview" PDF viewer (imagesc images appear
 "blurred" when printing to a PDF as a vector graphic and viewed using
 Preview).

 NOTE: The imagesc function assumes that each entry in a matrix gets the
 corresponding coordinate in 2-d space.  For example, entry (2,3) in the
 matrix is assigned to the coordinate with x = 3, y = 2 when using
 imagesc.  The pcolor function assumes that entries correspond to *edges*.
 So entry (2,3) in a matrix corresponds to the value between 2 and 3
 (along the x-axis) and between 1 and 2 (along the y-axis).  This is why
 one row and one column are cut off when using pcolor.  sanePColor
 behaves like imagesc (i.e. does not cut off data), but uses the "edge
 assignment" data representation required by pcolor.  sanePColor uses
 linear or logarithmic interpolation to infer the edges automatically.

 Usage: p = sanePColor([x,y],z,[logx],[logy]);

INPUTS:

    x: an array of sorted x values.  can also specify a min and max x value.
       these values correspond to columns of z. [IF THIS ARGUMENT IS USED,
       MUST ALSO SPECIFY Y VALUES.]
 
    y: an array of sorted y values.  can also specify a min and max y value.
       these values correspond to rows of z.  [IF THIS ARGUMENT IS USED,
       MUST ALSO SPECIFY X VALUES.]
 
    z: a 2d matrix of values.  this matrix determines the color at each
       point.
 
 logx: if this optional argument is set to true, the x-axis will plotted
       in log scale (similar to semilogx).

 logy: if this optional argument is set to true, the y-axis will plotted
       in log scale (similar to semilogy).

OUTPUTS:

    p: a handle to the resulting pcolor image.

 EXAMPLE:

   m = membrane;
   p = sanePColor(m);

 SEE ALSO: PCOLOR, IMAGE, IMAGESC, SEMILOGX, SEMILOGY, LOGLOG, PADARRAY

   AUTHOR: JEREMY R. MANNING
  CONTACT: manning3@princeton.edu

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

sanepcolor.m

SOURCE CODE ^

0001 function p = sanepcolor(varargin)
0002 %SANEPCOLOR  simple wrapper for pcolor
0003 %
0004 % Unlike the built-in pcolor command, this function does not "cut off" the
0005 % last row and column of the input matrix.  In this way, sanePColor is
0006 % intended to be as easy to use as imagesc, but allows the user to specify
0007 % the x and y coordinates of each cell if desired.  This function is also
0008 % useful as an alternative means of generating images to print to PDF that
0009 % are compatible with OS X's "Preview" PDF viewer (imagesc images appear
0010 % "blurred" when printing to a PDF as a vector graphic and viewed using
0011 % Preview).
0012 %
0013 % NOTE: The imagesc function assumes that each entry in a matrix gets the
0014 % corresponding coordinate in 2-d space.  For example, entry (2,3) in the
0015 % matrix is assigned to the coordinate with x = 3, y = 2 when using
0016 % imagesc.  The pcolor function assumes that entries correspond to *edges*.
0017 % So entry (2,3) in a matrix corresponds to the value between 2 and 3
0018 % (along the x-axis) and between 1 and 2 (along the y-axis).  This is why
0019 % one row and one column are cut off when using pcolor.  sanePColor
0020 % behaves like imagesc (i.e. does not cut off data), but uses the "edge
0021 % assignment" data representation required by pcolor.  sanePColor uses
0022 % linear or logarithmic interpolation to infer the edges automatically.
0023 %
0024 % Usage: p = sanePColor([x,y],z,[logx],[logy]);
0025 %
0026 %INPUTS:
0027 %
0028 %    x: an array of sorted x values.  can also specify a min and max x value.
0029 %       these values correspond to columns of z. [IF THIS ARGUMENT IS USED,
0030 %       MUST ALSO SPECIFY Y VALUES.]
0031 %
0032 %    y: an array of sorted y values.  can also specify a min and max y value.
0033 %       these values correspond to rows of z.  [IF THIS ARGUMENT IS USED,
0034 %       MUST ALSO SPECIFY X VALUES.]
0035 %
0036 %    z: a 2d matrix of values.  this matrix determines the color at each
0037 %       point.
0038 %
0039 % logx: if this optional argument is set to true, the x-axis will plotted
0040 %       in log scale (similar to semilogx).
0041 %
0042 % logy: if this optional argument is set to true, the y-axis will plotted
0043 %       in log scale (similar to semilogy).
0044 %
0045 %OUTPUTS:
0046 %
0047 %    p: a handle to the resulting pcolor image.
0048 %
0049 % EXAMPLE:
0050 %
0051 %   m = membrane;
0052 %   p = sanePColor(m);
0053 %
0054 % SEE ALSO: PCOLOR, IMAGE, IMAGESC, SEMILOGX, SEMILOGY, LOGLOG, PADARRAY
0055 %
0056 %   AUTHOR: JEREMY R. MANNING
0057 %  CONTACT: manning3@princeton.edu
0058 
0059 
0060 %CHANGELOG
0061 %3-16-10    JRM      Wrote it.
0062 %3-12-12    JRM      Support a more diverse range of input configurations.
0063 %9-21-12    JRM      Use linear and logistic interpolation to estimate data
0064 %                    coordinates more accurately.
0065 
0066 %parse arguments
0067 if length(varargin) == 1 %just z
0068     z = varargin{1};
0069     x = 1:size(z,2);
0070     y = 1:size(z,1);
0071     [logx,logy] = deal(false);    
0072 elseif (length(varargin) >= 4) %x, y, z, logx, and possibly logy
0073     x = varargin{1};    
0074     y = varargin{2};    
0075     z = varargin{3};    
0076     logx = varargin{4};
0077     if length(varargin) >= 5, logy = varargin{5}; else logy = false; end
0078 elseif length(varargin) == 2 %z and logx
0079     z = varargin{1};
0080     logx = varargin{2};
0081     assert(islogical(logx),'logx must be a logical');
0082     if logx
0083         x = logspace(log10(1),log10(size(z,2)),size(z,2));
0084     else
0085         x = 1:size(z,2);
0086     end
0087     logy = false;
0088     y = 1:size(z,1);
0089 else %length(varargin) == 3
0090     if isempty(varargin)
0091         fprintf('\nUsage: p = sanePColor([x,y],z,[logx],[logy]);\n');
0092         fprintf('Type ''help %s'' for more info.\n\n',mfilename);
0093         p = [];
0094         return;
0095     end
0096     %posibility 1: x, y, z
0097     if length(varargin{1}) > 1 && length(varargin{2}) > 1
0098         x = varargin{1};
0099         y = varargin{2};
0100         z = varargin{3};
0101         [logx,logy] = deal(false);
0102     %posibility 2: z, logx, and logy
0103     else
0104         z = varargin{1};
0105         logx = varargin{2};
0106         assert(islogical(logx),'logx must be a logical');
0107         if logx
0108             x = logspace(log10(1),log10(size(z,2)),size(z,2));
0109         else
0110             x = 1:size(z,2);
0111         end
0112         
0113         logy = varargin{3};
0114         assert(islogical(logy),'logy must be a logical');
0115         if logy
0116             y = logspace(log10(1),log10(size(z,1)),size(z,1));
0117         else
0118             y = 1:size(z,1);
0119         end        
0120     end
0121 end
0122 
0123 assert(length(x) == size(z,2),'length(x) must equal size(z,2)');
0124 assert(length(y) == size(z,1),'length(y) must equal size(z,1)');
0125 assert(islogical(logx),'logx must be a logical');
0126 assert(islogical(logy),'logy must be a logical');
0127 
0128 z = padarray(z,[1 1],'replicate','post');
0129 if logx
0130     newx = logexpand(x);
0131 else
0132     newx = linexpand(x);
0133 end
0134 
0135 if logy
0136     newy = logexpand(y);
0137 else
0138     newy = linexpand(y);
0139 end
0140 
0141 p = pcolor(newx,newy,z);
0142 shading flat;
0143 
0144 
0145 function[ey] = linexpand(y)
0146 x = 1:length(y);
0147 p = polyfit(1:length(y),y,1);
0148 
0149 ex = union(x - 0.5, x + 0.5);
0150 ex = prune(ex,length(ex) - (length(x) + 1));
0151 ey = polyval(p,ex);
0152 
0153 function[ex] = logexpand(x)
0154 ex = exp(linexpand(log(x)));
0155 
0156 function[x] = prune(x,n)
0157 d = diff(x);
0158 [~,ord] = sort(d);
0159 x(ord(1:n)) = [];
0160 
0161 %{
0162 License for sanePColor:
0163 Copyright (c) 2012, Jeremy
0164 All rights reserved.
0165 
0166 Redistribution and use in source and binary forms, with or without 
0167 modification, are permitted provided that the following conditions are 
0168 met:
0169 
0170     * Redistributions of source code must retain the above copyright 
0171       notice, this list of conditions and the following disclaimer.
0172     * Redistributions in binary form must reproduce the above copyright 
0173       notice, this list of conditions and the following disclaimer in 
0174       the documentation and/or other materials provided with the distribution
0175       
0176 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
0177 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
0178 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
0179 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
0180 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
0181 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
0182 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
0183 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
0184 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
0185 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
0186 POSSIBILITY OF SUCH DAMAGE.
0187 %}

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005