Alternative for readable but slow Table dot notation

Hi really like how legible the dot notation is for tables in Matlab... however it is just soooo slow to access and slows all my routines down.
I was wondering if there was an alternative to this?
Basically I was bundling up sets of data such as PhyProp, NumProp, GeomProp into tables and passing those to sub-functions.
At the top of each function I would unbundle the variables I needed:
say:
a = PhyProp.a
b = PhyProp.b
c = GeomProp.c
etc...
It made the function argument lists much more conscise and the code overall was still readable as all the unbundling always happened at the top of the function.
What I would like to avoid is to have to use indices, as it means you have to remember what they are before you can write new code.
for example if I had a cell array or something I could do something similar:
a = PhyProp{1}
b = PhyProp{2}
etc... but I have to remember that a is at index 1 etc...with tables it was just so much more readable.
I'll take any advice :)

5 个评论

Alternatives to consider:
  • table indexing using curly braces, e.g. PhyProp{'a'}
  • structure instead of table
  • nested functions instead of bundling/unbundling.
I have no idea if any of these are faster, you would need to test them.
Thanks @Stephen23. I tried the structure instead of table and that seems to work well. About 10 times faster than the table. Not a fast an indexed cell array, but might do for now as there is still a lot of active development so code legibility is a premium. In future I might revert to extended argument lists so I don't have to perform this operation deep inside the code.
"At the top of each function I would unbundle the variables I needed:"
a = PhyProp.a
b = PhyProp.b
c = GeomProp.c
etc...
Why create local copies of the same data? Just use the table variables as is.
I also have fallen into using table a lot and I haven't really seen that it has been a performance issue to do so -- but, I've not had really, really large tables to deal with; only a few hundred to thousands records. But, I studiously avoid such code as the above, so don't know if that is/is not part of the problem.
Have you profiled these routines to see where the time is shown up as being spent?
HI @dpb, thanks for your comment. Agree with you here, that local copies are not required. I did run the profiler on the code and 'readtable' and associated table calls are definitely hogging a lot of computation time. I was super surprised as my table is no more than 20 entries in a row, which is much less than you have.
"Why create local copies of the same data? Just use the table variables as is."
As long as the variables are not written to, then probably no local copies are made:
A local variable is just a handle to the existing data, without the repeating the indexing operation.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

版本

R2021b

提问:

2022-5-27

编辑:

2022-5-30

Community Treasure Hunt

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

Start Hunting!

Translated by