How to read a DLL from MATLAB

  • 18 October 2023
  • 0 replies
  • 99 views

Userlevel 6
Badge +2

Here shows a simple example to demonstrate how we can read a DLL from MATLAB. This can be useful for testing how the DLL works without really loading it into OpticStudio.

The steps to quickly go through this is as below

  1. Extract the zip file to a folder.
  2. Copy the grin1.dll from \Zemax\DLL\GradientIndex\ to the folder in step 1.
  3. Set up the position and parameters in the .m file as below.

     

  4. Run the MATLAB code and see the results as below.

     

Some key points.

(1) Need to prepare a .h file with same name as the DLL.

(2) Need to write the function you want to call in this .h file. You can just copy this from grin1.c file.

(3) Need to remove the APIENTRY keyword from the function.

(4) You need to convert the data to a pointer and pass it to the DLL.

(5) When reading it, you need to copy the data from the pointer again.

(6) This is only needed if the variable is supposed to be written by the DLL and you need to read back the data. Otherwise, you can simply pass the MATLAB array to the function call.

 

Enjoy!


0 replies

Be the first to reply!

Reply