Matlab printmat

前端 未结 3 1242
名媛妹妹
名媛妹妹 2021-01-14 03:54

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             


        
3条回答
  •  梦谈多话
    2021-01-14 04:41

    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)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

提交回复
热议问题