Performance comparison among Struct Array, Cell Array and Table

267 views (last 30 days)
Kat Lee
Kat Lee on 1 Nov 2018
Commented: Walter Roberson on 18 Mar 2022
I am facing an issue when to use what. There are three common way to store data in MATLAB: 1. Cell array; 2. Tables; 3. Struct arrays.
I did some search online for the performance among three of them: Struct will be the fastest, but still not really clear when to use what.
Can someone give me a general concept of the performance among these MATLAB data structures?
Thanks :)
  3 Comments
Michael O'Brien
Michael O'Brien on 18 Mar 2022
i found this super helpful due to the discussion it generated - thanks Kat Lee!

Sign in to comment.

Answers (3)

Matt J
Matt J on 1 Nov 2018
Edited: Matt J on 1 Nov 2018
They should all be about the same speed. If speed matters and the data is large, however, you shouldn't be using any of these. You should be storing data in numeric arrays instead. That way the data will be held contiguously in RAM and accessing it will be very fast.
  5 Comments
Bruno Luong
Bruno Luong on 6 Nov 2018
Especially when one can put the numerical array inside a struct with a meaningful fieldname.

Sign in to comment.


Bruno Luong
Bruno Luong on 2 Nov 2018
My general rule of thumbs:
  • Simple Array is the fastest
  • Using cell if you don't have a choice (mixing class or uniform sizes) and don't care about how to "name" elements.
  • Next recommendation is using struct of arrays and/or cell-arrays, that allows to have meaningful fieldnames, and flexible data exchanges.
  • Avoid at all cost array of structs for large number of records (said > 10), this will soon or later have big penalty of speed. I can't remember the last tile I use it, probably in my youth and never did it again.
  • Table is sort of Object Oriented built on top of CELL, personally I never feel a need to use it. I recognize it's very attractive for people who like excel sheet. ;-)
  5 Comments
Walter Roberson
Walter Roberson on 28 Nov 2019
Notice Peter's phrase, "at least not in the way that you probably mean."
In particular, many people tend to think that a table with N rows and V variables is stored as an N by V cell array, but instead it is stored as a struct that contains a 1 x V cell array each entry of which is an object with N rows.

Sign in to comment.


Peter Perkins
Peter Perkins on 6 Nov 2018
Kat, there's no way you are gonna get a useful answer without providing more information. The best representation of your data is gonna depend on your data and what you are doing, and how you plan on writing your code. Without knowing that, any answer is just guessing.
  5 Comments
Walter Roberson
Walter Roberson on 18 Mar 2022
Also, Mathworks has improved table() performance over the years.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by