Ideal way to access data in a table?
Afficher commentaires plus anciens
Is there a "prefered" way to access data in a table?
T.var1(1);
T{1,var1};
T{1,1};
3 commentaires
Stephen23
le 17 Août 2018
Use whatever syntax is clearest for your code.
Code is read more times than it is written. Complex code is harder to understand and has more bugs. Debugging is going to use more time than any possible speed benefits of any one of those syntaxes (if any). Therefore your aim is to make your code as clear as possible. Which syntax is clearest depends on what you are doing in your algorithm: it would make no sense to muck around with column names if your algorithm fundamentally depends on indices, and it would make no sense to muck around with indices if your algorithm fundamentally depends on column names. Use whichever is best for the situation.
All of those syntaxes are "prefered", in different situations.
Peter Perkins
le 24 Août 2018
+1 to Stephen, however, T{1,var1} would have to be T{1,'var1'}. Also, while I find T.var1(1) easier to follow than t{1,'var1'}, once you get to accessing the data in more than one variable in a table all at once, braces are the way to go: T{1,{'var1' 'var2'}}.
Walter Roberson
le 24 Août 2018
If you are importing from a csv or xls or xlsx, then a question becomes which you trust to be more accurate as files change: column number, or column header.
Some "standard" formats (even if only unofficial standards) define information according to column; others define by column names.
If your users expect to be able to insert columns at arbitrary points as long as they keep the column headers the same for what is already there, then you should probably be using column name.
Réponses (0)
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!