How do I pass structures between Visual Basic and MATLAB using a COM object built with MATLAB COM Builder?

2 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2018
Here is an example that shows how to correctly pass structures between MATLAB and Visual Basic through a COM object.
1. The following MATLAB code defines the structure within a function:
function Outlet = heatload3(Inlet)
Tin = Inlet.Temp;
Pin = Inlet.Press;
Mdot = Inlet.Mdot;
Q = Inlet.Q;
Outlet.Temp = Tin + 1.0;
Outlet.Press = Pin + 1.0;
Outlet.Mdot = Mdot-0.1;
Outlet.Q = Q-0.1;
2. You should then make a COM object of the above MATLAB code using MATLAB COM Builder (e.g. pheatloaddemo_1_0.dll).
3. In the Visual Basic code, there are eight text boxes. Four are used to initialize the input structure and four are used to display the results of the output structure.
The VB code is as follows:
Private theHeatload As pheatloaddemo.pheatload 'heatload object instantiation
Private Sub cmdCompute_Click()
' Declare the structure variables
Dim x As MWStruct
Dim y As MWStruct
' Create 1x1 struct arrays with no fields for x and y.
Set x = New MWStruct
Set y = New MWStruct
' Initialize Inlet and Outlet to have fields: Temp, Press, Mdot, and Q.
Call x.Initialize(1, Array("Temp", "Press", "Mdot", "Q"))
Call y.Initialize(1, Array("Temp", "Press", "Mdot", "Q"))
' Set input values to be sent to Matlab function, from text boxes
x.Item(1, "Temp") = Val(txtInletTemp.Text)
x.Item(1, "Press") = Val(txtInletPress.Text)
x.Item(1, "Mdot") = Val(txtInletMdot.Text)
x.Item(1, "Q") = Val(txtInletQ.Text)
' Call Matlab COM object function.
Call theHeatload.heatload3(1, y, x)
' Get output values from Matlab COM object function and display in text boxes
txtOutletTemp.Text = y.Item(1, "Temp")
txtOutletPress.Text = y.Item(1, "Press")
txtOutletMdot.Text = y.Item(1, "Mdot")
txtOutletQ.Text = y.Item(1, "Q")
End Sub
The next example demonstrates how to pass a two-level structure between MATLAB and VB.
1. The following MATLAB code defines the two-level field structure:
function z = heatload1(z)
Tin = z.Inlet.Temp;
Pin = z.Inlet.Press;
Mdot = z.Inlet.Mdot;
Q = z.Inlet.Q;
z.Outlet.Temp = Tin + 1.0;
z.Outlet.Press = Pin + 1.0;
z.Outlet.Mdot = Mdot-0.1;
z.Outlet.Q = Q-0.1;
2. Now, nake a COM object of the above MATLAB code using MATLAB COM Builder (e.g. mheatloaddemo_1_0.dll).
3. In the Visual Basic code there are eight text boxes. Four are used to initialize the input structure and four are used to display the results of the output structure.
The VB code is as follows:
Private theHeatload As mheatloaddemo.mheatload ' heatload object instantiation
Private Sub cmdCompute_Click()
' Declare the structure variables
Dim z As MWStruct
Dim x As MWStruct
Dim y As MWStruct
' Create 1x1 struct arrays with no fields for z, x, and y.
Set z = New MWStruct
Set x = New MWStruct
Set y = New MWStruct
' Initialize Inlet and Outlet to be 1x4 with fields Temp, Press, Mdot and Q.
Call z.Initialize(1, Array("Inlet", "Outlet")) ' Input/Output struct variable
Call x.Initialize(1, Array("Temp", "Press", "Mdot", "Q")) ' Input struct field
Call y.Initialize(1, Array("Temp", "Press", "Mdot", "Q")) ' Output struct field
' Initialize In and Out MWStructs
z(1, "In") = x
z(1, "Out") = y
' Set input values to be sent to Matlab function.
x(1, "Temp") = Val(txtInletTemp.Text)
x(1, "Press") = Val(txtInletPress.Text)
x(1, "Mdot") = Val(txtInletMdot.Text)
x(1, "Q") = Val(txtInletQ.Text)
' Call Matlab heatload1 function.
Call theHeatload.heatload1(1, z, z)
' Get output values from Matlab function.
txtOutletTemp.Text = y(1, "Temp").Value
txtOutletPress.Text = y(1, "Press").Value
txtOutletMdot.Text = y(1, "Mdot").Value
txtOutletQ.Text = y(1, "Q").Value
End Sub
If you are using MATLAB 7.1 (R14SP3) or later release, please see related solution 1-2CR00I.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!