1
$\begingroup$

I want to know what are the differences between two data structures in MATLAB: Structure Arrays and Cell Array.

In what circumstances we can use each of them?

$\endgroup$
1
  • $\begingroup$ I think that this question might get a better answer at Matlab forums. $\endgroup$
    – nicoguaro
    Commented Apr 25, 2016 at 21:36

2 Answers 2

2
$\begingroup$

Both structures and cell arrays are the most flexible data types built into matlab (as opposed to creating your own class), and that flexibility comes with a price - physical memory.

I'm not near a matlab-enabled machine, otherwise I would show you some examples; however, to get an idea of what I mean, create some data in matlab and then enter 'whos'. This will show you the memory usage of each data type. Cell arrays and structures are way more memory hungry than arrays, simply because you can combine different days types into a single variable (strings, booleans, numerics, etc.)

Now, to your question between cell arrays and structures, this is more of a question of preference in terms of writing, editing, and later understanding your code. I like structures because naming variable fields is easier to understand when multiple people are working on a project. Cell arrays take slightly more afterthought, because everything is based on a numeric index: what was cellData{:,2} again? It's easier with a structure: structData.address(:), oh ya those were addresses.

I'm a firm believer that matlab should be used so that it's as easy as possible to read and write. With bigger and more complicated projects, matlab itself becomes a bottleneck, and it would be better to write your code in another faster language.

$\endgroup$
2
$\begingroup$

As noted here, cell arrays and structs are both general enough to do the same things. However, one you access numerically (cell arrays) whereas the other you access fields by name (structs). In general, you choose the one that makes sense for readability. For example, if you want to loop through a bunch of things, probably put it in a cell array since the numeric indexing is convenient (though you can use structfun). If you want to store heterogeneous data in a convenient package to recall by name (such as different size matrices), then store it as a struct.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.