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 %
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); %
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