Search code examples
matlabmemorymatlab-table

What is the best way to preallocate a table in Matlab?


Usually I preallocate using cell(), zeros() or ones() depending on the type of data, but what is the best way to preallocate a table as it can hold various data structures?

I am talking about the table() functionality added in Matlab 2013b.

Obviously I can reserve memory using code like this:

T = table(cell(x,y))

but when my table is supposed to hold various datatypes I run into problems. Just imagine I want to fill in a column of integers now, or like in my case fill each row with an observation containing a string, an integer and a floating point number. T


Solution

  • How should Matlab know, how much memory to allocate, when you don't want to tell it what data is stored in the table? I don't think there is a good answer to your question besides "don't do it". If you know what is stored in each column, create the variables and add the rows as you go.

    Or create the Data in preallocated Matrixes/Cells and create the table from them at the end.