Visual Basicアプリケーションと Excel アドインの間で構造体の受け渡しはどのように行いますか?
11 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2018
Answered: MathWorks Support Team
on 27 Jun 2018
Visual Basic アプリケーションで、MATLAB Compiler で作成したExcel アドインを使用することを検討中です。
Visual Basic から Excel アドインへ構造体を渡したり、受け取ったりする方法を教えてください。
Accepted Answer
MathWorks Support Team
on 27 Jun 2018
以下は一般的な構造体の例です。
(1) 以下のようなMATLABコードを考えます。入力引数、出力引数のどちらも構造体です。
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) MATLAB Compuiler を使用して Excel アドインを作成します。(例:pheatloaddemo_1_0.dll)
(3) Visual Basic コードでは8つのテキストボックスがあります。4つは入力引数の構造体の設定用、その他の4つは出力引数の構造体の内容の表示用です。
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
次は2つの階層を持つ構造体についての例です。
(1) 以下のようなMATLABコードを考えます。こちらでは入出力引数の構造体は2つの階層を持っています。
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) MATLAB Compuiler を使用して Excel アドインを作成します。(例:mheatloaddemo_1_0.dll)
(3) Visual Basic コードでは8つのテキストボックスがあります。4つは入力引数の構造体の設定用、その他の4つは出力引数の構造体の内容の表示用です。
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
0 Comments
More Answers (0)
See Also
Categories
Find more on .NET と MATLAB 間でのデータ変換 in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!