Back to Code

2-D color plot

The normal matlab function pcolor didn't do what I wanted it to, e.g. it ignored final columns and rows. This version is what I expected the function to be: an n x m matrix is plotted as n x m squares, each square representing the data value at position (n, m).

function p = tg_pcolor(varargin)

% function tg_pcolor(Matrix)
% function tg_pcolor(Matrix, rowsValues, ColValues)
% function tg_pcolor(Matrix, rowsValues, ColValues, shading)
% 
% Improved pcolor function: an n x m matrix gives n * m squares.

mat = varargin{1};
[r, c] = size(mat);

if length(varargin) > 1,
    x = varargin{2}(:)';
    y = varargin{3}(:)';
else,
    x = 1:c;
    y = 1:r;
end;
x = [2 * x(1) - x(2) x];
y = [2 * y(1) - y(2) y];
if length(varargin) > 3,
    command = varargin{4};
    if strcmp(command, 'log') == 1,
        y = log(y);
    end;
end;

mat0 = [mat zeros(r, 1); zeros(1, c + 1)];
p = pcolor(x, y, mat0);

if length(varargin) > 3,
    shading(varargin{4});
else,
    shading('flat');
end;

xticklab = num2cell(x(2:end));
dx = diff(x) ./ 2;
tickx = x(1:(end - 1)) + dx;
set(gca, 'XTick', tickx);
set(gca, 'XTickLabel', xticklab);
yticklab = num2cell(y(2:end));
dy = diff(y) ./ 2;
ticky = y(1:(end - 1)) + dy;
set(gca, 'YTick', ticky);
set(gca, 'YTickLabel', yticklab);