Main Content

Pass Enumerated Types Examples

readEnumFunction

ThereadEnumfunction in theshrlibsamplelibrary displays a string that matches the input argument.

EXPORTED_FUNCTION char* readEnum(TEnum1 val) { static char outputs[][20] = { {"You chose en1"}, {"You chose en2"}, {"You chose en4"}, {"enum not defined"}, {"ERROR"} }; switch (val) { case en1: return outputs[0]; case en2: return outputs[1]; case en4: return outputs[2]; default : return outputs[3]; } return outputs[4]; }

The function signature is:

Return Type Name Arguments
cstring readEnum (Enum1)

The values for theEnum1input are defined in theshrlibsample.hheader file.

typedef enum Enum1 {en1 = 1, en2, en4 = 4} TEnum1;

Display Enumeration Values

This example shows how to pass enumeration values to thereadEnumfunction in theshrlibsamplelibrary. Load the library.

ifnot(libisloaded('shrlibsample')) addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample')end

In MATLAB, you can express an enumerated type as either the enumeration string or its equivalent numeric value. CallreadEnumwith a string argument.

calllib('shrlibsample','readEnum','en4')
ans = 'You chose en4'

CallreadEnumwith the equivalent numeric argument. TheEnum1definition declares enumerationen4equal to 4.

calllib('shrlibsample','readEnum',4)
ans = 'You chose en4'

Related Topics