image thumbnail
from display_matrix by Marshall
Plots the values of a matrix to a MatLab figure.

display_matrix(matrix, title_text, format)
function display_matrix(matrix, title_text, format)
%--------------------------------------------------------------------------
% display_matrix.m - Displays a matrix on the screen for easy viewing and
% printing.  For complex numbers, the 'format' string applies to both real
% and imaginary parts individually.
%
% Usage: display_matrix(matrix, title_text, format);
%
% Input:  M                      * MxN matrix to display
%         title  (optional)      * display title (if provided)
%         format (optional)      * number format (i.e. '%02.1f')
% Output: <matrix plot>
%
% Written by Marshall Crumiller
%--------------------------------------------------------------------------
% Matrix dimension check
if(length(size(matrix))>2), matrix=squeeze(matrix); end
if(length(size(matrix))>2)
    error('Matrix must be 1 or 2 Dimensional.');
end
matrix=transpose(matrix);
[M,N] = size(matrix);

% Check for multi-display setup, and determine which monitor is being used
%figure; hold on;
hold on;
scrsz = get(0,'MonitorPositions');
if(size(scrsz,1)>1)
    scrsz=sortrows(scrsz);
    x_loc=get(gcf,'outerposition');x_loc=x_loc(1);
    monitor = find(x_loc>(scrsz(:,1)'),1,'last');
    dims=scrsz(monitor,:);
    dims(3)=dims(3)-sum(scrsz(1:monitor-1,3));
    % all screens are top-aligned to the top of the tallest monitor
    max_height = max(scrsz(:,4));
    scrsz=dims;
else
    max_height=scrsz(4);
end
% figure out display proportion and matrix positioning
screen_width = scrsz(3);
screen_height = scrsz(4);
M=M+1; N=N+1;
set_width = screen_width*0.8;
set_height = screen_height*0.8;
center_x = scrsz(1)+screen_width/2;
center_y = (max_height-scrsz(4)+1)+screen_height/2;

% plot gridlines
gridlocs_x = 0:M; gridlocs_y = N:-1:0;
min_x = min(gridlocs_x); max_x = max(gridlocs_x);
min_y = min(gridlocs_y); max_y = max(gridlocs_y);
plot([gridlocs_x' gridlocs_x'],[min_y max_y],'k');
plot([min_x max_x],[gridlocs_y' gridlocs_y'],'k');

% thick lines separate headers
plot([gridlocs_x(2) gridlocs_x(2)],[min_y max_y],'k','linewidth',2);
plot([min_x max_x],[gridlocs_y(2) gridlocs_y(2)],'k','linewidth',2);

% determine text positioning and size
number_locs_x = gridlocs_x(1:end-1)+0.5;
number_locs_y = gridlocs_y(1:end-1)-0.5;
grid_size_y = set_height/M;
grid_size_x = set_width/N;
if(grid_size_y<grid_size_x)
    font_size=round(grid_size_y*0.35);
else
    font_size = round(grid_size_x*0.25);
end

% row header
for i = 1:M-1
    text(number_locs_x(i+1),number_locs_y(1),sprintf('%g',i),'fontsize',font_size,...
        'fontweight','bold','HorizontalAlignment','center','VerticalAlignment','middle');
end

% column header
for i = 1:N-1
    text(number_locs_x(1),number_locs_y(i+1),sprintf('%g',i),'fontsize',font_size,...
        'fontweight','bold','HorizontalAlignment','center','VerticalAlignment','middle');
end

if(~exist('format','var'))
    format='%1.2f';
end;

% scale font size to print format
if(~isreal(matrix)), font_size=font_size*0.5; end
default_num_digits=3;
num_digits=0;
before_decimal=true;
num_before_decimal=0;
for i = 1:length(format)
    c=format(i);
    if(c=='.'),before_decimal=false; end
    val=str2double(c);
    if(~isnan(val))
        num_digits=num_digits+val;
        if(before_decimal)
            num_before_decimal=num_before_decimal+val;
        end
    end
end
% find maximum matrix value, determine how it compares to
% num_before_decimal (needs extra scaling if it's larger)
m=ceil(log10(max([   max(max(real(matrix))) max(max(imag(matrix)))])));
if(m<1),m=1;end
d=m-num_before_decimal;
num_digits=num_digits+d;
scale_factor = default_num_digits/num_digits;
if(scale_factor>1),scale_factor=1; end
d=1-scale_factor; d=d*0.8; scale_factor=1-d;
font_size=round(font_size*scale_factor);

for i = 1:M-1
    for j = 1:N-1
        font_weight='normal';
        if(i==j), font_weight='bold'; end
        m=matrix(i,j);
        if(isreal(matrix(i,j)))
            text(number_locs_x(i+1),number_locs_y(j+1),sprintf(format,m),'fontsize',font_size,...
               'HorizontalAlignment','center','VerticalAlignment','middle','fontweight',font_weight);
        else
            if(imag(m)==0)
                text(number_locs_x(i+1),number_locs_y(j+1),sprintf(format,m),'fontsize',font_size,...
                   'HorizontalAlignment','center','VerticalAlignment','middle','fontweight',font_weight);
            else
                add_sign='+';
                if(imag(m)<0), add_sign='-'; end
                format_imag = sprintf('%s %s %si',format,add_sign,format);
                text(number_locs_x(i+1),number_locs_y(j+1),sprintf(format_imag,real(m),abs(imag(m))),'fontsize',font_size,...
                   'HorizontalAlignment','center','VerticalAlignment','middle','fontweight',font_weight);
            end
        end
    end
end

% Set labels & positioning
axis([0 M 0 N]);
set(gca,'xtick',[]);
set(gca,'ytick',[]);
y_pos = center_y-set_height/2;
y_height = set_height;
title_height = 50;
title_height_percent = 1;

% Display title if provided
if(exist('title_text','var') && ~isempty(title_text))
    y_pos = y_pos-title_height/2;
    y_height = y_height+title_height;
    title_height_percent = (y_height-title_height)/y_height;
    title(title_text,'fontsize',24);
end

set(gcf,'OuterPosition',[center_x-set_width/2 y_pos set_width y_height]);
set(gca,'position',[0 0 1 title_height_percent]);

Contact us