Matlab printmat

I am new to Matlab. Is there a way to use printmat to print two words?

Example result:

  Title One Title Two Title Three 11 22 33 22 33 44 

Here is the code I'm trying to change:

 matA = [ 11 22 33; 22 33 44]; printmat(matA, '' , '' , 'TitleOne TitleTwo TitleThree'); 

I cannot add a space between โ€œTitleโ€ and โ€œOne,โ€ where adding space always leads to the following result:

 printmat(matA, '' , '' , 'Title One Title Two Title Three'); Title One Title 11 22 33 22 33 44 

Any help is appreciated.

+4
source share
3 answers

According to Matlabs help, printmat will not provide what you are looking for. 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) 

The result is

 ans = Title One Title Two Title Three 11 22 22 33 33 44 

~ ~ edit
If using printmat is preferred (for example, since it is more flexible), you can work with evalc and strrep . The trick here is to replace the spaces with other characters (such as question marks) in the printmat call, save the output in a line through evalc , and then use strrep to replace the question marks with spaces. As a good by-product, you get the table as a row ...

 a = [ 11 22 33; 22 33 44]; x = evalc('printmat(matA, '''' , ''ab c'' , ''Title?One Title?Two Title?Three'')'); s = strrep(x, '?', ' ') 

The result is

 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 many apostrophes ...

+6
source

Well, printmat documentation tells you that

PRINTMAT (A, NAME, RLAB, CLAB) prints matrix A with RLAB row labels and CLAB column labels. NAME is a string used to indicate the name of a matrix. RLAB and CLAB are string variables that contain a string and column labels marked with spaces.

Therefore, spaces in the title are not supported.

As a workaround, you can use another delimiter that "looks like a space", for example, a block delimiter :

 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 can see, this becomes awkward very quickly. It also probably does not look correct when printing to a file or any other output than in the Matlab command window (for example, a terminal). You will have to experiment a bit.

Personally, I would just write my own, more general pretty-printer, using cell and sprintf with specific field widths in the format string, as suggested by H. Muster (+1).

+3
source

Another option is to override printmat as follows: What I did is add a new parameter to the function called separator , and you can call the printmat_v2 function using the separator you want between the headers. eg:

  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 
+1
source

All Articles