Search code examples
matlabcellcell-arraydisp

List cell contents in two columns of MATLAB cell array


I'm trying to display the contents of a cell array, which contain two columns, in a nice two column format in the command window.

tmp = [1:10]';
a{:,1} = tmp;
a{:,2} = dec2hex(tmp);
celldisp(a)

I would like the output to have the decimal values in the first column and hex values in the second column. Unfortunately I get:

celldisp(a)

a{1} =

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10

a{2} =

 1
 2
 3
 4
 5
 6
 7
 8
 9
 A

I am trying to get something that looks more like this:

desiredoutput

I also tried the table function but this gave:

wrongoutput


Solution

  • Use num2cell to place each element of a into a separate cell.

    disp([num2cell(a{1}) num2cell(a{2})]);
    
    %Output:
    %    [ 1]    '1'
    %    [ 2]    '2'
    %    [ 3]    '3'
    %    [ 4]    '4'
    %    [ 5]    '5'
    %    [ 6]    '6'
    %    [ 7]    '7'
    %    [ 8]    '8'
    %    [ 9]    '9'
    %    [10]    'A'