Working Example of a Matlab-Libtorch Interface


I wrote some sample code to interface MATLAB - PyTorch/Libtorch by the use of C++ and Libtorch.
I did it because for our application it was not an option to write to binary and load it (because of performance issues).
You can find the sample code here:

As I am rather new to C++, the code is by no means clean, but it (should) work.
The CMakeLists.txt builds an arbitrary shared dummy library as well as the actual interface which will be a .mex file for the use in MATLAB.


1 Like

Nice work and really useful. I had troubles on Mac OS x when using find_package(Matlab) in Cmake. I changed one line in your CmakeLists.txt and it worked flawless:


It is also possible to specify the Torch path directly if someone meets issues when including the Pytorch library:

find_package(Torch REQUIRED PATHS […YOUR PATH…]/libtorch)

1 Like