Code covered by the BSD License  

Highlights from
showcells

image thumbnail

showcells

by

 

25 Jan 2013 (Updated )

Display contents and structure of a cell array using ASCII boxes.

showcells (C)
function outstr = showcells (C)
%%SHOWCELLS  ASCII graphic representation of a cell array
%
%  This was inspired by the Dyalog APL display of boxed variables. It is
%  intended to provide a clear visual display of the contents of a cell
%  array. It uses NUM2STR to convert elements to strings.
%
%  SHOWCELLS(C); simply prints the visual representation to the console.
%  The semicolon suppresses the return of an integer representation of the
%  string to the console.
%
%  STR=SHOWCELLS(C); saves the generated char arrays to STR and
%  suppresses printing to the console. If C is 1D or 2D, STR is a char
%  array. If C is 3D or higher, STR is a cell array of char arrays.
%
%  Written by David Smith <david.smith@gmail.com>, 29 Jan 2013.
%
if ~iscell(C)
  error('Input must be a cell array.');
elseif isempty(C)
  C = {' '};
elseif ndims(C) > 2
  extra_dims = size(C);
  extra_dims = extra_dims(3:end);
  C = reshape(C,[size(C,1),size(C,2),prod(extra_dims)]);
  if isempty(inputname(1))
    var_name = 'ans';
  else
    var_name = inputname(1);
  end
end
[rows,cols,niters] = size(C);
for iter = 1:niters
  Cpart = cellfun(@cell2string, C(:,:,iter), 'UniformOutput', false);
  col_widths = zeros(cols,1);
  for kc = 1:cols
    col_widths(kc) =  max(cellfun(@(x) size(x,2)+3, Cpart(:,kc)));
  end
  row_heights = zeros(rows,1);
  for kr = 1:rows
    row_heights(kr) =  max(cellfun(@(x) size(x,1)+2, Cpart(kr,:)));
  end
  str = ' '*ones(sum(row_heights) + 1, sum(col_widths) + 1,'uint8');
  str(1,2:end-1) = '_';
  str(2:end-1,[1 end]) = '|';
  cur_row = 1;
  for kr = 1:rows
    cur_col = 1;
    for kc = 1:cols
      [dr,dc] = size(Cpart{kr,kc});
      str(cur_row,cur_col+1:cur_col+col_widths(kc)-1) = '_';
      str(cur_row+1:cur_row+row_heights(kr),cur_col) = '|';
      r = cur_row + ceil((row_heights(kr)-dr)/2) + 1;
      c = cur_col + floor((col_widths(kc)-dc)/2) + 1;
      str(r:r+dr-1,c:c+dc-1) = Cpart{kr,kc};
      cur_col = cur_col + col_widths(kc);
    end
    cur_row = cur_row + row_heights(kr);
  end
  str(end,:) = '_';
  str(end,cumsum([0; col_widths])+1) = '|';
  if ismatrix(C)
    outstr = char(str);
  else
    outstr{iter} = char(str);
  end
  if ~nargout
    if ndims(C) > 2
      subscripts = sprintf(',%d',ind2allsub(extra_dims,iter));
      fprintf('\n%s(:,:%s) = \n', var_name, subscripts);
    end
    indent = '    ';
    for k = 1:size(str,1)
      disp([indent, str(k,:)]);
    end
    fprintf('\n');
  end
end

function S = cell2string(C)
%%CELL2STRING Convert individual element to strings.
if iscell(C)
  S = showcells(C);
else
  S = num2str(C,'%-0.3g ');
end

function subs = ind2allsub (dims, index)
%%IND2ALLSUB Returns array of subscripts when nargout = 1
n = length(dims);
subs = zeros(1,n);
k = [1 cumprod(dims(1:end-1))];
for j = n:-1:1
  vi = rem(index-1, k(j)) + 1;
  subs(j) = (index - vi)/k(j) + 1;
  index = vi;
end

Contact us