Main Content

Pass Enumerated Types Examples

readEnum Function

The readEnum function in the shrlibsample library 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 TypeNameArguments
cstringreadEnum(Enum1)

The values for the Enum1 input are defined in the shrlibsample.h header file.

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

Display Enumeration Values

This example shows how to pass enumeration values to the readEnum function in the shrlibsample library. Load the library.

if not(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. Call readEnum with a string argument.

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

Call readEnum with the equivalent numeric argument. The Enum1 definition declares enumeration en4 equal to 4.

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

Related Topics