What is the fastest way to add a row at the bottom of a table?
51 views (last 30 days)
Show older comments
Hi all,
In my program, I create an empty table with, lets say, 70 columns. Lets call it 'Collection_Table'.
During a tipical run the program has to add a one row table, 'Row_Table', to the bottom of 'Collection_Table'.
In a tipical run it happens few tens of thousend times (~20000 and more).
It turns that it takes A LOT OF TIME. I tryied the following methods:
1.
Collection_Table(end+1,:) = Row_Table;
2.
Collection_Table = [Collection_Table; Row_Table];
3.
I created a very large 'Cellection_Table' with zero, or empty, entires to eliminate the problem of enlarging the table during run.
Then, I used a counter 'Counter' to track the right entry to the table:
Counter = Counter + 1;
Collection_Talbe(Counter,:) = Row_Table;
In all three methods the time it takes to add 'Row_Table' data was long! Actually it is the major time consuming action in the program.
Is there a way to be more efficient? Am I missing something? Or I have to accept that working with the tables is time consuming?
Thanks,
Alon
1 Comment
Akira Agata
on 11 Dec 2018
If you know a maximum number of rows, say N, of Collection_Talbe after running your program, how about pre-alocating a N-by-70 empty table ?
To pre-alocate a table, the following example will be some help.
Answers (3)
Guillaume
on 11 Dec 2018
"Or I have to accept that working with the tables is time consuming?"
You have to accept that growing anything (tables, matrices, etc.) one row at a time is time consuming. For efficiency data is stored continuously in memory. Since the memory after your current matrix/table may already be in use, when you add a row, matlab has to:
- allocate a new block of memory somewhere with enough room for the current data + the new row
- copy over the current data into that new block
- add the new row at the end
Do that a few 20,000 times and it adds up to a lot of memory allocations and data copies.
The way to avoid is not to grow things but tell matlab from the start what the final size is going to be by preallocating your matrix/table/whatever, and then filling it one row at a time. If for some reason, there's no way to know beforehand the final size, then preallocate a too large table/whatever, fill it up and trim the extra at the end.
8 Comments
Guillaume
on 16 Dec 2018
I'm a bit confused. Whichever way you create the table, at some point you must convert these structures into something that can be put into a table. So how did you do it originally?
Perhaps this is that conversion that is actually the bottleneck.
I suggest that you give an example of the data you have. If everything is a structure, perhaps concatenating the lot into a single structure array instead of a cell array is the way to go. You can then convert that structure array into a table with struct2table.
Charles Lee
on 13 Sep 2020
My solution is to set all variables as 'char' before merging.
- detectImportOptions() to find the variables with type 'double'
- setvartype() to set the variables as 'char'
- Just merge
%% one example→merge Sapfluxnet_info & St_md :
opts = detectImportOptions('Sapflux_St_md.csv','NumHeaderLines',0);
% Just give a hint in case it can't get the variablenames
opts = setvartype(opts,{'st_age','st_basal_area','st_density','st_height'},'char');
%'st_age','st_basal_area','st_density','st_height' → 'double' type variables
Sapfluxnet_info = readtable('Sapflux_St_md.csv',opts);
opts = detectImportOptions('St_md_name.csv','NumHeaderLines',0);
opts = setvartype(opts,{'st_age','st_basal_area','st_density','st_height'},'char');
St_md = readtable('St_md_name.csv',opts);
Sapfluxnet_info = [Sapfluxnet_info;St_md];
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!