Struct vs Table - which is better

117 Ansichten (letzte 30 Tage)
Rizwan Khan
Rizwan Khan am 29 Sep. 2020
Verschoben: Voss am 11 Dez. 2024 um 16:39
Hello,
Can someone tell me where i can read the pro's and con's of Structs Vs Tables
So i'm reading in data from an API, i can store it in a struct, and if i want i can also store it in a table.
Which of these two are easier to work with later on?
So i will need to do mathematical calculations, plot graphs, do use loops functions, all sorts of stuff with the data.
So i'm wondering what is going be easier to use?
What is going to be more memory efficient and speed efficient?
Your thoughts will be appreciated

Antworten (2)

Image Analyst
Image Analyst am 29 Sep. 2020
A matrix is best and fastest if you just have numbers everywhere. If you have tabular data where different columns have different data types, like col 1 is scalar numbers, col 2 is strings, and col 3 is vectors, etc. then you should use a table. Tables can have different data types in every column - double matrices cannot. But all elements of a particular column mut be the same, so can't have a double in row 1 and a string in row 2.
If you need maximum flexibility, at the cost of speed and overhead, then you can use a cell array, which has no requirements that data types match up within rows or columns. Basically with a cell array, anything can be anywhere. See the FAQ: FAQ: What is a cell array?
Structures are pretty similar to tables but are probably less efficient than a table. They are more flexible than a table though, for example one structure might have only some of the fields that another structure in the same structure array has - but it's pretty rare that you'd have that situation.
Bottom line: I recommend you become better acquainted with tables. (I assume you know everything about regular numerical matrices of course.)

KSSV
KSSV am 29 Sep. 2020
  4 Kommentare
Rizwan Khan
Rizwan Khan am 29 Sep. 2020
Verschoben: Voss am 11 Dez. 2024 um 16:39
Hello,
Yes I did not know about matrix until your post above
What are your thoughts on Matrix, are they easy to read data into and out of?
Are they easy to use in for loops
Are they easy to perform calculations on the total set ?
John Wolter
John Wolter am 11 Dez. 2024 um 16:33
Verschoben: Voss am 11 Dez. 2024 um 16:39
A matrix is just a 2-D array. They are very simple complared to other data structures such as cell arrays, structs, tables. You can read/write data to them with a simple assignment.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by