Arthur Tarasov
Arthur Tarasov

Reputation: 3791

How to create a table with NaNs in Matlab?

I am trying to create a table that is 10 x 5 with only NaNs. I start by creating an array with NaNs:

N = NaN(10, 5);

then I try converting it to a table:

T = table(N);

It puts all cells into one column, but I need the table to be 5 columns with one NaN in each cell. Does anyone know how to do that?

Upvotes: 1

Views: 5453

Answers (2)

rayryeng
rayryeng

Reputation: 104555

array2table works just fine. This takes a matrix and converts it to the table structure where each column of the matrix is a column in the output table:

>> N = NaN(10, 5);
>> T = array2table(N)

T = 

    N1     N2     N3     N4     N5 
    ___    ___    ___    ___    ___

    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN
    NaN    NaN    NaN    NaN    NaN

Upvotes: 8

Matthew Gunn
Matthew Gunn

Reputation: 4529

What you want is:

t = array2table(NaN(10,5))

Bonus (so our answers are slightly different :P) You can rename the variables to anything you want with something like:

t.Properties.VariableNames = {'x1','x2','x3','x4','x5'};

Upvotes: 5

Related Questions