3

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
florian
  • 604
  • 8
  • 31

1 Answers1

2

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.

Mathias
  • 1,470
  • 10
  • 20
  • Hmm good point. In my case I know what type of data each column will be, for instance the column "name" will always contain strings. I then add various rows to the table(amount depending on the data) with each iteration of the loop. So if I understand you correctly I should create variables with the preallocated data. for instance a long cell() column for the names and include that in the table. However I don't know how many rows there will be in the end. Is it a good idea to overestimate then and remove the empty rows in the end? – florian Apr 08 '14 at 10:19
  • I can't give you a good recommendation on this. It Depends on how much you overestimate. I mean, if you preallocate cells, they won't allocate space for your strings, because how should Matlab know how long they are? I guess reshaping the cells won't take much time compared to allocating the string. Unless you have like 100k entries perhaps. Still if performance is an issue it might be better to reallocate 10k cells at a time then allocating 300k and only using 100k. – Mathias Apr 08 '14 at 10:25
  • Just tested here: `a= cell(10000000,1)'; is probably fast enough and doesn't take much memory either. So overestimating will probably not be a problem unless you go way beyond that – Mathias Apr 08 '14 at 10:28