4

我是 Matlab 的新手。有没有办法用来printmat打印 2 个单词的标题?

示例结果如下:

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

这是我目前尝试修改的代码:

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

我似乎无法在“标题”和“一个”之间添加空格,添加空格总是会导致以下结果:

printmat(matA, '' , '' , 'Title One Title Two Title Three');

     Title              One               Title
        11               22                  33
        22               33                  44

任何帮助表示赞赏。

4

3 回答 3

6

根据 Matlabs 的帮助,printmat不会提供你正在寻找的东西。你可以sprintf改用。

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)

这导致

ans =

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

~edit~
如果使用printmat更可取(例如,因为它更灵活),您可以使用evalcand来解决strrep。这里的技巧是在对 的调用中用其他符号(例如问号)替换空格,printmat通过 将输出存储在字符串中evalc,然后用strrep空格替换问号。作为一个不错的副产品,您可以将表格作为字符串...

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

s = strrep(x, '?', ' ')

这导致

s =


                 Title One    Title Two  Title Three
            a     11.00000     22.00000     33.00000
            b     22.00000     33.00000     44.00000

但是 printmat 和 evalc 的组合会导致很多撇号......

于 2013-02-25T15:10:04.740 回答
3

好吧,文档printmat告诉你

PRINTMAT(A,NAME,RLAB,CLAB) 打印带有行标签 RLAB 和列标签 CLAB 的矩阵 A。NAME 是用于命名
矩阵的字符串。RLAB 和 CLAB 是字符串变量,包含
由空格分隔的行和列标签。

所以标题中的空格不是原生支持的。

作为一种解决方法,您可以使用另一个“看起来像空格”的分隔符,例如,单位分隔符

printmat (
    matA, '', 'one two', ...
    ['Title' char(31) 'One Title' char(31) 'Two Title' char(31) 'Three']);

输出:

Test = 
           Title One    Title Two  Title Three
    one     11.00000     22.00000     33.00000
    two     22.00000     33.00000     44.00000

但是正如你所看到的,这很快就会变得很尴尬。当打印到文件或其他输出而不是 Matlab 命令窗口(例如终端)时,它也可能看起来不正确。你必须做一些实验。

正如 H.Muster (+1) 所建议的那样,我个人只会使用cells 和格式字符串中的特定字段宽度编写自己的更通用的漂亮打印机。sprintf

于 2013-02-25T15:10:16.220 回答
1

另一种选择是重新定义printmat如下:我所做的是向名为 的函数添加一个新参数,您可以使用标题之间的分隔符separator调用该函数。printmat_v2例如:

 printmat_v2 (matA, ' ' , ' ' , 'Title OneL Title TwoL Title Three','L');

代码:

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
于 2013-02-25T15:19:54.340 回答