Main Content

Call Library Functions on Windows

In the previous step, you built a MATLAB® interface to the C++ matrixOperations library in the matrixlib folder.

Building interface file 'matrixlibInterface.dll' for clib package 'matrixlib'.
Interface file 'matrixlibInterface.dll' built in folder 'C:\Users\Documents\MATLAB\matrixlib'.

To use the library, add the interface file folder to the MATLAB path.
addpath('C:\Users\Documents\MATLAB\matrixlib')

Set MATLAB Path to Interface Folder

Add the MATLAB interface file to the MATLAB path.

addpath('matrixlib')

Copy Library File to Interface Folder

Put the shared library file in the folder with the MATLAB interface file. Uncomment and execute one of these statements based on your selected compiler.

%copyfile(fullfile(productPath,"win64","mingw64","*.dll"),"matrixlib","f")
%copyfile(fullfile(productPath,"win64","microsoft","*.dll"),"matrixlib","f")

View Help

At the MATLAB command prompt, type these commands to open documentation for the library in your help browser.

doc clib.matrixlib.Mat  %load the package
doc clib.matrixlib      %display package members

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

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

Call Library Functions

To create a Mat object and call functions in the library, type:

matObj = clib.matrixlib.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

Share Interface

To give the interface to another MATLAB user, create a toolbox installation (.mltbx) file. Using the instructions in Distribute MATLAB Interface to C++ Library:

  • Set the toolbox folder to your matrixlib folder which contains interface file matrixlibInterface.dll and shared library matrixOperations.dll.

  • Identify the package name (calling syntax) as clib.matrixlib.