Call Functions in Linux Interface to C++ Shared Library

If you created the matrixOperations interface in the example Publish Interface to Shared C++ Library on Linux, then you can use it in the following example. The example assumes the matrixOperations.dll interface file is in folder with your user name:

/mathworks/home/user/MATLAB/publisher/matrixexample/myPkg

Set Paths

At the operating system prompt, add the path to the C++ shared library file. For more information, see Set Run-Time Library Path for C++ Interface.

setenv LD_LIBRARY_PATH rtPath

where rtPath is the output of:

rtPath = fullfile(fullfile(matlabroot,'extern','examples','cpp_interface'),'glnxa64')

Start MATLAB®. Add the MATLAB interface file to the MATLAB path.

% Replace user with your user name
user = "myname"; % Replace "myname" with your user name;
addpath("/mathworks/home/"+user+"/MATLAB/publisher/matrixexample/myPkg")

View Help

At the MATLAB command prompt, display help for the interface. In the example, the clibgen.generateLibraryDefinition command changed the name of the interface to matrixOperations to myPkg. Type this command to load the package.

doc clib.myPkg.Mat

To display the members of the package, type:

doc clib.myPkg
Classes contained in clib.myPkg:
Mat             - clib.myPkg.Mat    Representation of C++ class Mat

Functions contained in clib.myPkg:
addMat          - clib.myPkg.addMat    Representation of C++ function addMat
updateMatByX    - clib.myPkg.updateMatByX    Representation of C++ function updateMatByX
updateMatBySize - clib.myPkg.updateMatBySize    Representation of C++ function updateMatBySize

To display signatures for the package function, click the links for addMat, updateMatByX, and updateMatBySize.

clib.myPkg.addMat    Representation of C++ function addMat
  inputs
    mat            read-only clib.myPkg.Mat
  outputs
    RetVal         int32

clib.myPkg.updateMatByX    Representation of C++ function updateMatByX
  inputs
    mat            clib.myPkg.Mat
    X              int32
  outputs

clib.myPkg.updateMatBySize    Representation of C++ function updateMatBySize
  inputs
    mat            clib.myPkg.Mat
    arr            int32
  outputs

To display information about class clib.myPkg.Mat, click the link for Mat.

clib.myPkg.Mat    Representation of C++ class Mat
Method Summary:
  Mat                 - clib.myPkg.Mat    Constructor of C++ class Mat
  Mat                 - clib.myPkg.Mat    Constructor of C++ class Mat
  setMat              - clib.myPkg.Mat.setMat    Method of C++ class Mat
  getMat              - clib.myPkg.Mat.getMat    Method of C++ class Mat
  getLength           - clib.myPkg.Mat.getLength    Method of C++ class Mat
  copyMat             - clib.myPkg.Mat.copyMat    Method of C++ class Mat

To display constructor and method signatures, use the methods or methodsview functions. For example, type:

methodsview clib.myPkg.Mat

Call Library Functions

Test the functions in the interface. For example, type:

matObj = clib.myPkg.Mat;   % Create a Mat object
intArr = [1,2,3,4,5];
matObj.setMat(intArr);     % Set the values to intArr
retMat = matObj.getMat(5)  % Display the values
retMat =

  1×5 int32 row vector

   1   2   3   4   5

Related Topics