Home > Mobile >  Define Multiple Enums in a Single File in Matlab
Define Multiple Enums in a Single File in Matlab

Time:09-17

Is it possible to define multiple enumerations in a single Matlab file? Or is it possible to have "local" enums in the same way we can define local funtions at the end of a file?

I am working on a project where it would be handy to have multiple enumeration classes, but it is annoying to use a classdeff every time because it requires a separate file, and this means that there are lots of short files whose sole purpose it is to define enumerations. At the moment, each enumeration looks like this:

classdef exampleEnumType < uint32
    
    enumeration
        zero(0),
        one(1),
        two(2),
    end

end

Is there a way to compactly define enumerations in Matlab so that I do not need a separate file for each (using Matlab 2021a)?

CodePudding user response:

First of all, having lots of short files is the normal situation in MATLAB. Working around that is possible but oftentimes futile.

Traditionally in MATLAB, constant values are defined as a function. For example, pi is a function. It looks something like this:

function v = pi
   v = 3.14159

By making the constant into a function, it is available from everywhere without first running code to define those constants.

An enumeration is nothing more than a series of constants. The same can be accomplished with a struct:

exampleEnumType = struct('zero',0, 'one',1, 'two',2);

Since fairly recently (R2019b), MATLAB allows dot indexing into the output of a function call, but you do need to use empty parenthesis in the function call. Thus, we can declare the above struct the same way we did with constants:

function v = exampleEnumType
   v = struct('zero',0, 'one',1, 'two',2);

This allows to do exampleEnumType().zero (almost) like with the enum or with a struct variable.

So how do we extend this to defining multiple of these in a single file? If a common prefix is no problem, we can define a class with static member functions, each one declaring a constant:

classdef constants
   methods(Static)
       function v = pi
          v = 3.14159
       end
       function v = exampleEnumType
          v = struct('zero',0, 'one',1, 'two',2);
       end
   end
end

We now have constants.pi and constants.exampleEnumType().zero.

Alternatively, create a single function that returns a more complex struct as follows:

function v = constants
   v.pi = 3.14159;
   v.exampleEnumType = struct('zero',0, 'one',1, 'two',2);

This allows us to do constants().exampleEnumType.zero.

Note that the above will not work in the same way for MATLAB releases prior to R2019b. For older versions of MATLAB, the last method (a function constants) would be the best approach. The user would just need to do constants = constants; at the top of any function needing to use the constants. This shadows the function with a variable of the same name, so that constants.exampleEnumType.zero works as expected.

CodePudding user response:

You can use a map...

exampleEnum = containers.Map( {'zero','one','two'}, {0,1,2} );

usage looks like

exampleEnum('zero') % = 0
  • Related