4

Matlabは初めてです。printmat2単語の見出しを印刷するために使用する方法はありますか?

結果の例は次のとおりです。

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

これが私が現在変更しようとしているコードです:

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

「タイトル」と「1」の間にスペースを追加できないようです。スペースを追加すると、常に次の結果になります。

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て回避できます。ここでの秘訣は、の呼び出しでスペースを他の記号(疑問符など)に置き換え、出力をを介して文字列に格納してから、を使用して疑問符をスペースに置き換えることです。素晴らしい副産物として、あなたはテーブルを文字列として取得します...evalcstrrepprintmatevalcstrrep

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

もう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 に答える