Home > utility > matPlot.m

matPlot

PURPOSE ^

matPlot: Display a matrix in a figure window.

SYNOPSIS ^

function matPlot(a, matrixName, gridColor, fontSize, fontColor, rowLabel, colLabel);

DESCRIPTION ^

 matPlot: Display a matrix in a figure window.
    Usage: matPlot(a, matrixName, gridColor, fontSize, fontColor, rowLabel, colLabel);

    For example:
        matPlot(magic(10));

    Roger Jang, 20071009

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function matPlot(a, matrixName, gridColor, fontSize, fontColor, rowLabel, colLabel);
0002 % matPlot: Display a matrix in a figure window.
0003 %    Usage: matPlot(a, matrixName, gridColor, fontSize, fontColor, rowLabel, colLabel);
0004 %
0005 %    For example:
0006 %        matPlot(magic(10));
0007 %
0008 %    Roger Jang, 20071009
0009 
0010 if nargin<1, selfdemo; return; end
0011 if nargin<2, matrixName=''; end
0012 if nargin<3, gridColor='k'; end
0013 if nargin<4, fontSize=10; end
0014 if nargin<5, fontColor='b'; end
0015 if nargin<6,
0016     for i=1:size(a, 1)
0017         rowLabel{i}=int2str(i);
0018     end
0019 end
0020 if nargin<7,
0021     for i=1:size(a, 2)
0022         colLabel{i}=int2str(i);
0023     end
0024 end
0025 
0026 % Clear the current axis
0027 cla reset;
0028 [m,n]=size(a);
0029 
0030 % Place the text in the correct locations
0031 % Index over number of rows
0032 for rowCnt=1:m,
0033     % Index over number of columns
0034     for colCnt=1:n,
0035         numberString=num2str(a(rowCnt,colCnt));
0036         text(colCnt-.5,m-rowCnt+.5,numberString, ...
0037             'HorizontalAlignment','center', ...
0038             'Color','b', ...
0039             'FontWeight','bold', ...
0040             'FontSize',fontSize);
0041     end;
0042 end;
0043 
0044 for rowCnt=1:m,
0045     text(-0.1, m-rowCnt+.5, rowLabel{rowCnt}, ...
0046         'HorizontalAlignment','right', ...
0047         'Color','r', ...
0048         'FontWeight','bold', ...
0049         'FontSize',fontSize);
0050 end;
0051 
0052 for colCnt=1:n,
0053     text(colCnt-.5, m+.1, colLabel{colCnt}, ...
0054         'HorizontalAlignment','left', ...
0055         'rot', 90, ...
0056         'Color','r', ...
0057         'FontWeight','bold', ...
0058         'FontSize',fontSize);
0059 end;
0060 
0061 
0062 
0063 set(gca,'Box','on', ...
0064         'Visible','on', ...
0065         'xLim',[0 n], ...
0066         'xGrid','on', ...
0067         'xTickLabel',[], ...
0068         'xTick',0:n, ...
0069         'yGrid','on', ...
0070         'yLim',[0 m], ...
0071         'yTickLabel',[], ...
0072         'yTick',0:m, ...
0073         'DataAspectRatio',[1, 1, 1], ... 
0074         'GridLineStyle',':', ...
0075         'LineWidth',3, ...
0076         'XColor',gridColor, ...
0077         'YColor',gridColor);
0078 
0079 xlabel(matrixName);
0080 
0081 % ====== Self demo
0082 function selfdemo
0083 mat=magic(11);
0084 feval(mfilename, mat, 'Magic Matrix');

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003