Get enum string - matlab coder

I have an enumeration like this:

classdef(Enumeration) bla_type < int32 enumeration bla_one(1) bla_2(2) end end 

I can get the "string representation of the element" as follows:

 char(bla_type.bla_one) 

=>

 bla_one 

Unfortunately, the Matlab encoder does not like. Are there any alternatives?

+7
source share
2 answers

There is no elegant built-in way to do this in Coder; an enumerated type becomes a standard enumeration in C, and the enumeration function in MATLAB is not available in the encoder. The easiest but unpleasant way to do this is to create a function using the switch statement with manually populated names. This is not nice, since now you have to maintain the names in two places.

However, one of the ways that really works is to use one of Coder's more powerful features: coder.const.

The solution is to have a function that creates a table of enumeration elements and their values. This function itself cannot be compiled, but rather, is called at compile time to build a lookup table in the resulting C code. We can use this lookup table in a function compatible with the encoder to retrieve data.

Suppose we have a numbered type (in some part of .m):

 classdef someenum < int32 %#codegen enumeration First_thing (0) Second_thing (2) Another_thing (3) No_thing (4000) end end 

We also have a build-time function called "buildsomeenum2name.m":

 function [namearray, memberidx] = buildsomeenum2name %BUILDSOMEENUM2NAME Compile-time creation of lookup table for someenum % THIS FUNCTION IS NOT CODER COMPATIBLE, BUT IS CALLED DURING COMPILE % TO CREATE A LOOKUP TABLE. [members, names]=enumeration('someenum'); maxlen = 0; for i=1:numel(names) maxlen = max(maxlen, numel(names{i})); end namearray = char(zeros(numel(names), maxlen)); for i=1:numel(names) namearray(i, 1:numel(names{i})) = names{i}; end memberidx = int32(members); %#ok<NASGU> end 

When buildsomeenum2name is called in MATLAB, it creates an array of string names of all members of the enumerated type and another vector list of their numeric values ​​in the same order.

Here's the cool part. MATLAB Coder can evaluate functions during assembly and turn them into constants. These constants become literals in the resulting C code, not the actual code. Since functions are evaluated at build time, the enumeration information is put into a nice table, so if we create a search function compatible with Coder, we can use it to convert member types to strings. We will call this function "someenum2name.m":

 function name = someenum2name(enum) %#codegen %SOMEENUM2NAME Get the string name of an enumerated type % The following line loads namearray and memberidx with constant arrays coder.extrinsic('buildsomeenum2name'); [namearray, memberidx] = coder.const(@buildsomeenum2name); % First find the index of the enumerated type in the memberidx vector index = find(memberidx==int32(enum)); if isempty(index) name = 'UNKNOWN'; return; end name = deblank(namearray(index,:)); end 

This function uses the coder.const command to evaluate buildsomeenum2name at compile time and create lookup tables. We should instruct Coder not to try to compile buildsomeenum2name , so use the coder.extrinsic command to tell her to ignore this function. Then someenum2name can search for the index for the string and pull it out (deblank is used because the lines in the array have an end 0 that needs to be pulled out.) The someenum2name function can be called both inside MATLAB and the compiler has compiled the code.

This method keeps everything in sync, so if you ever add a new member to an enumeration or reorder them, the coder.const function will ensure that the values ​​are rearranged in the output code, so someenum2name works.

On the command line, it looks like this:

 >> someenum2name(someenum.No_thing) ans = No_thing 
+2
source

Try [~,s]=enumeration('bla_type') . You get an array of row cells containing the name of the elements in s . So bla_one will be in s{1} . Not sure if this is supported by MATLAB codes.

0
source

All Articles