How do I integrate a DLL generated from Simulink into Excel VBA?

9 vues (au cours des 30 derniers jours)
How do I integrate a DLL generated from Simulink into Excel VBA?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 15 Juil 2024
Modifié(e) : MathWorks Support Team le 15 Juil 2024
Please follow the steps below to use a DLL file generated from a Simulink model in Excel VBA.
1. Create a Simulink model
2. Set the system target file to ert_shrlib.tlc.
3. Implement address return functions in the Custom Code section
Address return functions refer to functions within the DLL that allow VBA to retrieve the memory addresses of external input and output structures. Through these functions, VBA can directly access and modify the data in the structures.
Since VBA cannot directly reference the memory addresses of C/C++ structures, the DLL must provide functions that return the addresses of these structures. This enables VBA to manipulate the structures via their addresses.
Implementation Method
In addition to the provided C code, implement the get_rtU_address and get_rtY_address functions to return the addresses of the external input (rtU) and external output (rtY) structures. Please refer to the following example of address return functions:
/* Address return functions */
__declspec(dllexport) ExtU* get_rtU_address() {
return &rtU;
}
__declspec(dllexport) ExtY* get_rtY_address() {
return &rtY;
}
Add these functions to Configuration Parameters -> Code Generation -> Custom Code section
4. Write a VBA Script
'Declare DLL functions with PtrSafe
Declare PtrSafe Sub vba_sample_initialize Lib "vba_sample_win64.dll" ()
Declare PtrSafe Sub vba_sample_step Lib "vba_sample_win64.dll" ()
Declare PtrSafe Sub vba_sample_terminate Lib "vba_sample_win64.dll" ()
'Declare external input structure
Public Type ExtU
Inport1 As Double
Inport2 As Double
End Type
'Declare external output structure
Public Type ExtY
Outport1 As Double
End Type
'Declare external input and output variables
Public rtU As ExtU
Public rtY As ExtY
'Declare functions to get the addresses of external input and output variables with PtrSafe
Declare PtrSafe Function get_rtU_address Lib "vba_sample_win64.dll" () As LongPtr
Declare PtrSafe Function get_rtY_address Lib "vba_sample_win64.dll" () As LongPtr
'Declare CopyMemory function with PtrSafe (Windows API)
Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
Sub CallSimulinkModel()
'Initialize
vba_sample_initialize
'Set inputs from Excel cells
rtU.Inport1 = Sheets("Sheet1").Range("A1").Value
rtU.Inport2 = Sheets("Sheet1").Range("A2").Value
'Pass the address of rtU to the DLL
Dim rtUAddress As LongPtr
rtUAddress = get_rtU_address()
CopyMemory ByVal rtUAddress, rtU, LenB(rtU)
'Execute one step
vba_sample_step
'Get output values from the address of rtY
Dim rtYAddress As LongPtr
rtYAddress = get_rtY_address()
CopyMemory rtY, ByVal rtYAddress, LenB(rtY)
'Display the result
MsgBox "The result of adding " & rtU.Inport1 & " and " & rtU.Inport2 & " is " & rtY.Outport1
'Terminate
vba_sample_terminate
End Sub
You need to enter the above VBA code in Excel by going to the Developer tab -> Visual Basic -> Module. To enable the Developer tab, go to File -> Options -> Customize Ribbon in Excel, and check the Developer.
5. Run the VBA script 
In Step 4, we wrote the code to fetch input values from Excel cells and perform calculations as shown below. After entering values into the specified cells, click the Macro button and run the VBA script.
' Set inputs from Excel cells
rtU.Inport1 = Sheets("Sheet1").Range("A1").Value
rtU.Inport2 = Sheets("Sheet1").Range("A2").Value

Plus de réponses (0)

Catégories

En savoir plus sur Data Export to MATLAB dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by