Main Content

Pass String Arguments Examples

stringToUpper Function

The stringToUpper function in the shrlibsample library converts the characters in the input argument to uppercase. The input parameter, char *, is a C pointer to a string.

EXPORTED_FUNCTION char* stringToUpper(char *input) 
{
   char *p = input;

   if (p != NULL)
      while (*p!=0)
         *p++ = toupper(*p);
   return input;
}

The function signature for stringToUpper is shown in the following table. MATLAB® maps the C pointer type (char *) into cstring so you can pass a MATLAB character array to the function.

Return TypeNameArguments
[cstring,
cstring]
stringToUpper(cstring)

Convert MATLAB Character Array to Uppercase

This example shows how to pass a MATLAB character array str to a C function, stringToUpper.

str = 'This was a Mixed Case string';

Load the library containing the stringToUpper function.

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

Pass str to the function.

res = calllib('shrlibsample','stringToUpper',str)
res = 
'THIS WAS A MIXED CASE STRING'

The input parameter is a pointer to type char. However, a MATLAB character array is not a pointer, so the stringToUpper function does not modify the input argument, str.

str
str = 
'This was a Mixed Case string'

Related Topics