matlabmatlab-table

making a Table in Matlab


I'm trying to make a table in Matlab and initialze it with zeros so I did it in this way:

z =zeros(4,4);
exTab = table(z,'RowNames',{'row1' 'row2' 'row3' 'row4' }, 'VariableNames',{'column','column2','column3','column4'}) 

and I received this error

The VariableNames property must contain one name for each variable in the table

it recognizes all columns of the zeros as just one column, how to do it in the right way?


Solution

  • Use array2table instead of table. i.e.

    z = zeros(4,4);
    exTab = array2table(z,'RowNames',{'row1','row2','row3','row4'},...
        'VariableNames',{'column1','column2','column3','column4'});
    

    This, as expected, gives:

    >> exTab
    exTab =
      4×4 table
                column1    column2    column3    column4
                _______    _______    _______    _______
        row1       0          0          0          0   
        row2       0          0          0          0   
        row3       0          0          0          0   
        row4       0          0          0          0