Code covered by the BSD License  

Highlights from
mat2fig - Conversion from a matrix to a figure

image thumbnail

mat2fig - Conversion from a matrix to a figure

by

 

Function for converting a matrix into a figure (eps,fig,jpg,etc.) and latex code.

mat2fig(A,varargin)
% Function for converting a matrix into a figure + latex code.
% varargout = mat2fig(A,varargin) - conversion from matrix to a figure
%
% INPUT:    A - input matrix
%           optional:
%               'FileName'  - name of the file. Type: eps(default), jpg, png,...
%               'MatName'   - matrix name: 'on','off',"name"
%               'align'     - alignment of the elements in the matrix: 'l','c','r'
%               'bracket'   - type of bracket: '(','[','{','|','\|'
%
% OUTPUT:   latex code (optional)
%
% example:
%   mat2fig([1 2;3 4]);   
%   mat2fig([1 2;3 4],'MatName','B','bracket','[');   
%   mat2fig([1 2;3 4],'FileName','Matrix');
%   mat2fig([1 2;3 4],'FileName',{'Matrix','jpg'});
%   mat2fig([1 2;3 4],'MatName','A_{b}');
%   mat2fig([1 2;3 4],'MatName','A_{b}','align','r','bracket','\|');
%   mat2fig([1 2;3 4],'FileName',{'A.eps','psc2'},'MatName','A_{b}','align','r','bracket','\|');
%
% In LATEX amsmath package is required (due to \setcounter{MaxMatrixCols}{}).  
%
% Known BUGS:
%   1. Matrices of size bigger than 10 cannot be converted (because of latex interpreter in matlab)! For cases of >10 only latex code
%   is produced.
%   2. Figures are sometimes cut and not correctly bounded (eg. case of jpg
%   format). Restarting matlab can possibly help.
%
% 18 Mar 2012   - created:  Ondrej Sluciak <ondrej.sluciak@nt.tuwien.ac.at>
%%%%
function varargout = mat2fig(A,varargin)

if (~isnumeric(A))
    error('Incorrect input matrix!');
end

if (nargin>1 && mod(nargin,2)==0 || nargin>9)
    error('Incorrect number of parameters');
end

[n,m] = size(A);
MaxSize = 10;   %Matrix of max. 10 columns can be converted to eps (due to matlab restriction on latex interpreter)

B = cell(2*n*m-1,1);
k = 1;
for i=1:n
    for j=1:m
        B{k} = num2str(A(i,j));
        if (j==m)
            if (i~=n)
                B{k+1} = '\cr';
            end
        else
            B{k+1} = '&';
        end
        k = k+2;
    end
end

fig = figure('Visible','off');

filename{1} = 'matrix';
filename{2} = [];

if nargin>1
   [align,bracket,mname,filename] = processInput(inputname(1),varargin);
   align = char(align*ones(1,m));
   if (isempty(mname))
       str2 = ['\left',bracket(1,:),'\!\!\!\begin{array}{',align,'}',strcat(B{:}),'\end{array}\!\!\!\right',bracket(2,:)];
       str = ['$\hspace{-0.3cm}',str2,'\hspace{-0.3cm}$'];
       
       a = annotation('textbox','Position',[0.1482 0.7333 0.17 0.16], 'FitBoxToText','on','String',str,'interpreter',...
            'latex','Margin',-4,'VerticalAlignment','middle','HorizontalAlignment','center');
   else
       str2 = [mname,'=\left',bracket(1,:),'\!\!\!\begin{array}{',align,'}',strcat(B{:}),'\end{array}\!\!\!\right',bracket(2,:)];
       str = ['$\hspace{-0.1cm}',str2,'\hspace{-0.3cm}$'];
       
       a = annotation('textbox','Position',[0.1482 0.7333 0.17 0.16], 'FitBoxToText','on','String',str,'interpreter',...
            'latex','Margin',-4,'VerticalAlignment','middle','HorizontalAlignment','center');
   end
else
    str2 = ['\left(\!\!\!\begin{array}{ccc}',strcat(B{:}),'\end{array}\!\!\!\right)'];
    str = ['$\hspace{-0.3cm}',str2,'\hspace{-0.3cm}$'];
    
    a = annotation('textbox','Position',[0.1482 0.7333 0.17 0.16], 'FitBoxToText','on','String',str,'interpreter',...
        'latex','Margin',-4,'VerticalAlignment','middle','HorizontalAlignment','center');
end

if (n<MaxSize && m<MaxSize)
    set(a,'Units','pixels');
    aPos = get(a,'Position');
    fPos = get(fig,'Position');
    fPos(3:4)=aPos(3:4);
    set(fig,'Position',fPos,'Units','norm','Paperpositionmode','auto');
    set(a,'Units','norm','Position',[0 0 1 1],'LineStyle','none')
else
    warning('mat2fig:mat2fig','Matrix size exceeded!. Producing latex code only...');
end

if (nargout>0)
    varargout{1} = str2;
end

if (n<MaxSize && m<MaxSize)
    if (isempty(filename{2}))
        print(fig,'-deps2',filename{1});
    else
        saveas(fig,filename{1},filename{2});
    end
end    
    
texfile = filename{1};
dot = find(texfile=='.');
texfile(dot:end) = [];
texfile = [texfile,'.tex'];
fid = fopen(texfile,'w');
fprintf(fid, '%s',['\setcounter{MaxMatrixCols}{',num2str(m),'}']); %amsmath package in latex is required!
fprintf(fid, '$%s$', str2);
fclose(fid);

end


function [align,bracket,mname,filename] = processInput(matname,params)

n = numel(params);
align = 'c';

bracket(1,:) = '(';
bracket(2,:) = ')';
mname = [];
filename = cell(1,2);
if (matname~='')
    filename{1} = matname;
else 
    filename{1} = 'matrix';
end
for i=1:2:n
    if (ischar(params{i}));
        switch lower(params{i})
            case 'align'
                switch lower(params{i+1})
                    case {'l','r','c'}
                        align = lower(params{i+1});
                    otherwise
                        error('Incorrect alignment type: l,c,r'); 
                end
            case 'bracket'
                switch lower(params{i+1})
                    case '(';
                        bracket(1,1) = '(';
                        bracket(2,1) = ')';
                    case {'{','['}
                        bracket(1,1) = lower(params{i+1});
                        bracket(2,1) = char(lower(params{i+1})+2);
                    case '|'
                        bracket(1,1) = lower(params{i+1});
                        bracket(2,1) = lower(params{i+1});
                    case '\|'
                        bracket(1,1:2) = lower(params{i+1});
                        bracket(2,1:2) = lower(params{i+1});
                    otherwise
                        error('Incorrect bracket type: (,[,{,|,\|'); 
                end
            case 'matname'
                switch lower(params{i+1})
                    case 'on'
                        if (~isvarname(matname))
                            error('Unknown variable name.');
                        end
                        mname = matname;
                    case 'off'
                        mname = [];
                    otherwise
                        if (ischar(params{i+1}))
                            mname = params{i+1};
                        else
                            error('Incorrect matrix name parameter: on, off, "name" '); 
                        end
                end
            case 'filename'
                sas = params{i+1};
                m = size(params(i+1),2);
                if (iscell(sas))
                    m = size(sas,2);
                end
                switch m
                    case 1
                        filename{1} = sas;
                        filename{2} = [];
                    case 2
                        filename{1} = sas{1};
                        filename{2} = sas{2};
                    otherwise
                        error('Incorrect filename parameters.');
                end
            otherwise
                error(['Incorrect input parameter: ',params{i}]);
        end
    end
end

end

Contact us