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.