frankundfrei
frankundfrei

Reputation: 325

MATLAB: printmat with complex numbers

I want to use MATLAB's printmat do display a matrix with labels. But this doesn't work for complex numbers:

N = 5; 

x = rand(N,1);
y = rand(N,1);
z = x + 1i*y;

printmat([x,y,z],'fftdemo','N=1 2 3 4 5','x y x+iy');

Output:

fftdemo = 
                     x            y         x+iy
      N=1      0.84072      0.34998      0.84072
        2      0.25428      0.19660      0.25428
        3      0.81428      0.25108      0.81428
        4      0.24352      0.61604      0.24352
        5      0.92926      0.47329      0.92926

As you can see the imaginary part of z isn't printed.

Is there a way to get Matlab to display complex numbers or another way to achieve this?

Upvotes: 0

Views: 679

Answers (2)

Chris
Chris

Reputation: 470

Here is a slightly altered version of printmat that will print complex numbers. Feel free to fiddle around a bit more with it for better looks :)

function [] = printmat2(a,name,rlab,clab)
%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 1986-2002 The MathWorks, Inc. 
%   $Revision: 1.10 $  $Date: 2002/04/10 06:32:35 $

error(nargchk(1,4,nargin));

space = ' ';
[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, sprintf('--%d--> ',i)]; end
  for i=1:ncols, clab = [clab, sprintf('--%d--> ',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(sprintf('%s = ',name)), end
  disp(' ')
  disp('     []')
  disp(' ')
  return
end

% Remove extra spaces (delimiters)
ndx1 = find(clab==' ');
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==' ');
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(sprintf('%s = ',name)), end  % Print name
while col<=ncols
  % Print labels
  s = space(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=[space(ones(1,len-length(lab))),lab]; end
    s= [s,' ',lab];
  end
  disp(setstr(s))
  for i=1:nrows,
      s = rlab(rpos(i)+1:rpos(i+1)-1);
      if length(s)>len, s=s(1:len); else s=[space(ones(1,len-length(s))),s]; end
      s = [' ',s];
      for j=0:n,
          element = a(i,col+j);
          if imag(element) ~= 0
              s=[s,sprintf('%12.5f + %12.5fi',[real(element) imag(element)])];
          else
              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
      end
    disp(s)
  end % for
  col = col+col_per_scrn;
  disp(' ')
  if (ncols-col<n), n=ncols-col; end
end % while

% end printmat

Upvotes: 2

Mohit Jain
Mohit Jain

Reputation: 863

Any print function in matlab will only print real value of imaginary number. TO get both the parts you must call them explicitly.So correct usage would be

 printmat([x,y,real(z),imag(z)],'fftdemo','N=1 2 3 4 5','x y x iy');

But this wont be of any use now since both are parts are getting printed twice.

Upvotes: 2

Related Questions