I am working with some code that uses values from an enumeration
class (extending uint8
) to access matrix columns "by name" rather than by a hard-coded number. The reason for this approach is that matrices are perceived as more performant compared to other constructs that allow named field access (such as structs/object or tables) - which supposedly improves code readability without sacrificing performance.
The trouble is that the enum class is found inside a package, giving it a rather long name. This hurts readability quite significantly to the point it almost defeats the purpose of "named indices":
% What it looks like without using an enum:
val = foo(:, 7); % Magic number, :(
% Current situation (the enum is called "somePackage.SomeMeaningfulCollectionEnum"):
val = foo(:, somePackage.SomeMeaningfulCollectionEnum.varName1); % Very long name, :(
% Desired solution:
E = somePackage.SomeMeaningfulCollectionEnum; % This throws an error in R2020a
val = foo(:, E.varName1); % Short and descriptive!
When attempting to use the "desired solution" above, we get the following error:
Error using somePackage.SomeMeaningfulCollectionEnum
Cannot call the constructor of 'somePackage.SomeMeaningfulCollectionEnum' outside of its enumeration block.
I have come up with a workaround consisting of storing each enumeration member in a struct
field with the same name (see my own answer below), but I'm wondering if there's a "proper" way to do this.