Main Content

Handle Updated Array Elements

If a C++ function updates the elements of an array parameter, then the function also updates the MATLAB® clib array because the array is a MATLAB handle.

Display Help for Interface

Suppose that you have an interface with function fcn. To run this example, follow the instructions in Build Interface for Update Array Elements Example to generate a MATLAB interface named libUpdateArray.

When you display help for libUpdateArray, you see that it contains .

help clib.libUpdateArray.fcn
fcn -  clib.libUpdateArray.fcn Representation of C++ function fcn.

  clib.libUpdateArray.fcn(doubleArr)
    Input Arguments
      doubleArr      vector clib.array.libUpdateArray.Double  

Use in MATLAB

Call this function from MATLAB to update the array elements with their values multiplied by 2.

doubleArr = clibConvertArray("clib.libUpdateArray.Double", [2 3 4]); 
clib.libUpdateArray.fcn(doubleArr); 
doubleArr(3) % display element 3
ans = 
     8

Build Interface for Update Array Elements Example

This C++ header file defines a function that modifies the elements of the parameter doubleArr.

void fcn(double * doubleArr, int size) { 
    for(int i = 0; i < size; ++i) 
        doubleArr[i] *= 2.0; 
}

To run the example, save this code in a header file named updateArray.hpp, then generate a MATLAB interface named libUpdateArray following the instructions in Header-Only HPP File.

To create interface libUpdateArray, edit the definelibUpdateArray.m definition file. Uncomment the statements defining fcn and replace <SHAPE> with "size".

Library ArtifactsMATLAB Interface libnameMATLAB Help

Header file updateArray.hpp

clib.libUpdateArray

>> help clib.libUpdateArray