How can I use Python in Matlab for dll usage?

13 visualizaciones (últimos 30 días)
Christopher Headley
Christopher Headley el 7 de Jun. de 2024
Respondida: Rushikesh el 8 de Ag. de 2024
I am using Python inside of MATLAB to load a DLL so I can use its functionality. I have tried almost every combination of inputs to try and make this MATLAB-Python combo work, but I have been unsuccessful. I know my DLL is good because I have used it using solely python and I have also gotten it working in MATLAB using a different set of commands, but I really want to use the MATLAB/Python combo.
Below is what I am trying to do
%% Using Python
ctypes = py.importlib.import_module('ctypes');
vnx = ctypes.cdll.LoadLibrary('file path where dll and header files are stored \vnx_fmsynth.dll');
%% Calling these fx's don't work!
vnx.fnLMS_SetTestMode(0)
vnx.fnLMS_GetNumDevices()
I can assure fnLMS_SetTestMode and fnLMS_GetNumDevices, are methods within the DLL but python-matlab does not want to recognize them. I have tried over 100 combinations of trying to get the DLL to function, but I simply cannot figure out the correct syntax. When I run essentially the same lines in python however, it does work, so I am obviously doing something wrong inside of matlab.
Let me show you what does work in MATLAB with out using python.. (this does require you have a C-code compiler installed on your PC).
%% Load Libraries
cd('file path where dll and header files are stored')
libName='vnx_fmsynth';
loadlibrary([libName,'.dll'],'vnx_LMS_api.h')
libfunc= libfunctions('vnx_fmsynth')
%% tab complete for the lazy
for i=1:length(libfunc)
libF.(libfunc{i})=libfunc{i}; % this way the auto complete from the structure's field is the same as the library name
end
%% Calling these fx's work!
calllib(libName, 'fnLMS_SetTestMode',0)
devNum=calllib(libName,libF.fnLMS_GetNumDevices)
I have included the files in .zip for you to try yourself.
the fnLMS_GetNumDevices function should response with '0'. the TestMode function doesn't respond with anything, but it does functionally work. I have been told and have watched videos that running python inside of MATLAB works the same, but I am starting to question my presumptions. :( Any and all help would be very much appreciated!!
I am using MATLAB 2022A

Respuestas (1)

Rushikesh
Rushikesh el 8 de Ag. de 2024
I understand that you are trying to run python code inside MATLAB attempting to access functions inside DLL file. Your presumptions are true, MATLAB does support python, but it comes with few limitations.
Currently MATLAB does not support calling methods from object using dot operator. It is mentioned in MATLAB R2024a documentation that functions starting with underscore (_) are not supported and seems like same issue persists with normal function calling using object as well. Here is the link of Limitations to Python support by MATLAB to get more insights.
There is simple workaround I have found, we can use "py.getattr(Object, ‘prop’)" function to get
fnLMS_SetTestMode’ and ‘fnLMS_GetNumDevices’ functions of ‘vnx’ object.
Here is sample code I have tried on MATLAB R2022a and it is working as expected (same output in python environment)
% Path to your DLL file
dll_path = 'path to your dll \vnx_fmsynth.dll';
% Import the ctypes module
ctypes = py.importlib.import_module('ctypes');
% Load the DLL
vnx = py.ctypes.CDLL(dll_path);
fnLMS_SetTestMode = py.getattr(vnx, 'fnLMS_SetTestMode');
fnLMS_SetTestMode.argtypes = {ctypes.c_int}; % Use ctypes.c_int
fnLMS_SetTestMode.restype = ctypes.c_int;
% Call the function
fnLMS_SetTestMode(int32(2));
fnLMS_GetNumDevices = py.getattr(vnx, 'fnLMS_GetNumDevices');
fnLMS_GetNumDevices.argtypes = {};
fnLMS_GetNumDevices.restype = ctypes.c_int;
num_devices = fnLMS_GetNumDevices();
% Display the result
disp("DLL Loaded")
disp(num_devices)
It shows correct and expected output tested using inputs such as [0, 2] as attached below.
Also there are several things we need to keep in mind,
  1. Each MATLAB version supports limited pythons versions. So, we need to make sure that python version we are using is compatible with MATLAB version. List could be found below - https://www.mathworks.com/support/requirements/python-compatibility.html?s_tid=srchtitle_site_search_3_python%2520support
  2. I would recommend to set argument and return types of methods of object from python environment, that we are accessing using MATLAB. It ensures that arguments and return types are correctly interpreted.
Example Code:
fnLMS_SetTestMode.argtypes = {ctypes.c_int}; % Use ctypes.c_int
fnLMS_SetTestMode.restype = ctypes.c_int;
Let me know if it helps. Thanks

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by