I am new to Matlab. Is there a way to use printmat
to print 2 words heading?
Example result as followed:
Title One Title Two
Well, the documentation of printmat
tells you that
PRINTMAT(A,NAME,RLAB,CLAB) prints the matrix A with the row labels RLAB and column labels CLAB. NAME is a string used to name the
matrix. RLAB and CLAB are string variables that contain the row
and column labels delimited by spaces.
So spaces in the title are not natively supported.
As a workaround, you can use another separator that "looks like a space", for example, the unit separator:
printmat (
matA, '', 'one two', ...
['Title' char(31) 'One Title' char(31) 'Two Title' char(31) 'Three']);
output:
Test =
Title One Title Two Title Three
one 11.00000 22.00000 33.00000
two 22.00000 33.00000 44.00000
But as you see, this gets awkward real fast. It will also probably not look right when printed to file or some other output than the Matlab command window (terminal, for instance). You'll have to experiment a bit.
Personally I would just write my own, more general pretty-printer using cell
s and sprintf
with specific field-widths in the format string, as suggested by H.Muster (+1).
Another option is to redefine printmat
as follows:
What I've done is add a new parameter to the function called separator
, and you can call the printmat_v2
function with the separator you want between titles.
e.g:
printmat_v2 (matA, ' ' , ' ' , 'Title OneL Title TwoL Title Three','L');
Code:
function [] = printmat_v2(a,name,rlab,clab,separator)
%PRINTMAT Print matrix with labels.
%
% PRINTMAT(A,NAME,RLAB,CLAB) prints the matrix A with the row labels
% RLAB and column labels CLAB. NAME is a string used to name the
% matrix. RLAB and CLAB are string variables that contain the row
% and column labels delimited by spaces. For example, the string
%
% RLAB = 'alpha beta gamma';
%
% defines 'alpha' as the label for the first row, 'beta' for the
% second row and 'gamma' for the third row. RLAB and CLAB must
% contain the same number of space delimited labels as there are
% rows and columns respectively.
%
% PRINTMAT(A,NAME) prints the matrix A with numerical row and column
% labels. PRINTMAT(A) prints the matrix A without a name.
%
% See also: PRINTSYS.
% Clay M. Thompson 9-24-90
% Copyright (c) 1986-93 by the MathWorks, Inc.
error(nargchk(1,5,nargin));
[nrows,ncols] = size(a);
if nargin<2, name = []; end
if nargin==3, error('Wrong number of input arguments.'); end
if nargin<4,
rlab = []; clab = [];
for i=1:nrows, rlab = [rlab, '--',int2str(i),'--> ']; end
for i=1:ncols, clab = [clab, '----',int2str(i),'---- ']; end
rlab=rlab(1:length(rlab)-1);
clab=clab(1:length(clab)-1);
end
col_per_scrn=5;
len=12;
if (nrows==0)|(ncols==0),
if ~isempty(name), disp(' '), disp([name,' = ']), end
disp(' ')
disp(' []')
disp(' ')
return
end
% Remove extra spaces (delimiters)
ndx1 = find(clab==separator);
ndx2 = find([ndx1,0]==[-1,ndx1+1]);
if ~isempty(clab), clab(ndx1(ndx2))=[]; end
ndx1 = find(rlab==' ');
ndx2 = find([ndx1,0]==[-1,ndx1+1]);
if ~isempty(rlab), rlab(ndx1(ndx2))=[]; end
% Determine position of delimiters
cpos = find(clab=='L');
if length(cpos)<ncols-1, error('Not enough column labels.'); end
cpos = [0,cpos,length(clab)+1];
rpos = find(rlab==' ');
if length(rpos)<nrows-1, error('Not enough row labels.'); end
rpos = [0,rpos,length(rlab)+1];
col=1;
n = min(col_per_scrn-1,ncols-1);
disp(' ')
if ~isempty(name), disp([name,' = ']), end % Print name
while col<=ncols
% Print labels
s = ' '*ones(1,len+1);
for j=0:n,
lab = clab(cpos(col+j)+1:cpos(col+j+1)-1);
if length(lab)>len,
lab=lab(1:len);
else
lab=[' '*ones(1,len-length(lab)),lab]; end
s= [s,' ',lab];
end
disp(s)
for i=1:nrows,
s = rlab(rpos(i)+1:rpos(i+1)-1);
if length(s)>len, s=s(1:len); else s=[' '*ones(1,len-length(s)),s]; end
s = [' ',s];
for j=0:n,
element = a(i,col+j);
if element==0,
s=[s,' 0'];
elseif (element>=1.e6)|(element<=-1.e5)|(abs(element)<.0001)
s=[s,sprintf(' %12.5e',element)];
else
s=[s,sprintf(' %12.5f',element)];
end
end
disp(s)
end % for
col = col+col_per_scrn;
disp(' ')
if (ncols-col<n), n=ncols-col; end;
end % while
According to Matlabs help, printmat
will not provide what you are seeking. You can use sprintf
instead.
a = [ 11 22 33; 22 33 44];
s = {'Title One' 'Title Two' 'Title Three'};
s1 = sprintf('%12s\t%12s\t%12s\t\n', s{:});
s2 = sprintf('%12d\t%12d\t%12d\n', a);
horzcat(s1,s2)
This results in
ans =
Title One Title Two Title Three
11 22 22
33 33 44
~edit~
If the use of printmat
is preferable (e.g., because it is more flexible) you can work around by using evalc
and strrep
. The trick here is to replace the spaces with other symbols (e.g. question marks) in the call to printmat
, store the output in a string via evalc
, and then use strrep
to replace the question marks by spaces. As a nice byproduct, you get the table as a string...
a = [ 11 22 33; 22 33 44];
x = evalc('printmat(matA, '''' , ''a b c'' , ''Title?One Title?Two Title?Three'')');
s = strrep(x, '?', ' ')
This results in
s =
Title One Title Two Title Three
a 11.00000 22.00000 33.00000
b 22.00000 33.00000 44.00000
However, the combination of printmat and evalc causes a lot of apostrophes...