How to pass arguments to a deployed DLL within .NET, if the arguments are of type struct and not straight numerical arrays?
2 次查看(过去 30 天)
显示 更早的评论
I have to call a Matlab DLL in Visual Studio .NET that was deployed by MATLAB Coder (R15) for .NET. I found examples for how to do this if the arguments of the Matlab function are simple numerical arrays (eg. addMatrix(a,b) ). But in my case the function arguments are structures of nummerical arrays, and I hat no luck until now to call the function without getting a stack error.
The function call in Matlab is this:
[AIR, Result, ErrorCode] = myFkt(AIR, PWR, Control);
And the arguments in Matlab look like this:
AIR = struct;
AIR.Vdot_h = [2993 0 0 2993];
AIR.Vdot = [0.83138888888888884 0 0 0.83138888888888884];
AIR.T = [25 0 -3.4 0];
AIR.rF = [0.6 0 0.78299999999999992 0];
AIR.p = [101325 0 101325 0];
PWR = struct;
PWR.definition = [1 1 2 100 1 2.5 640 0 0 0 0 0 0 0 0 0 0 0 0 0];
PWR.geometry = [0.94 0 135 25 25 0 0 0 0 0];
PWR.physical = [60 1.5898298 60 1.5898298 235 2.4028 62.347 46.339 0 2500 1.3308 0.9537 0 0 0 0 0 0 0 0];
PWR.parameter = [1.12 0.94 1.12 0.94 1 1 0.041 0.07 0.0225 -1 0.6 0 0.768 1.1 0.85 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0];
PWR.Acoustic_input = [0 0 0 0 0 0 0 0 0 0 0 0];
Control = [0 4 0 1 0 0 0 0 -0 1 0 0 0 1 0 1 0 1 0 3];
The function returns 3 objects:
AIR = struct of arrays
Result = numerical array
ErrorCode = float
The .NET code I tried to call the DLL: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim obj As myFkt.Class1 = Nothing
Dim AIR As MWStructArray
Dim AIRFields As [String]() = {"Vdot_h", "Vdot", "T", "rF", "p"}
AIR = New MWStructArray(1, 5, AIRFields)
AIR("Vdot_h", 1) = New MWNumericArray({2993, 0, 0, 2993})
AIR("Vdot", 1) = New MWNumericArray({0.83138888888888884, 0, 0, 0.83138888888888884})
AIR("T", 1) = New MWNumericArray({25, 0, -3.4, 0})
AIR("rF", 1) = New MWNumericArray({0.6, 0, 0.78299999999999992, 0})
AIR("p", 1) = New MWNumericArray({101325, 0, 101325, 0})
Dim PWR As MWStructArray
Dim PWRFields As [String]() = {"definition", "geometry", "physical", "parameter", "Acoustic_input"}
PWR = New MWStructArray(1, 5, PWRFields)
PWR("definition", 1) = New MWNumericArray({1, 1, 2, 100, 1, 2.5, 640, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
PWR("geometry", 1) = New MWNumericArray({0.94, 0, 135, 25, 25, 0, 0, 0, 0, 0, 0})
PWR("physical", 1) = New MWNumericArray({60, 1.5898298, 60, 1.5898298, 235, 2.4028, 62.347, 46.339, 0, 2500, 1.3308, 0.9537, 0, 0, 0, 0, 0, 0, 0, 0})
PWR("parameter", 1) = New MWNumericArray({1.12, 0.94, 1.12, 0.94, 1, 1, 0.041, 0.07, 0.0225, -1, 0.6, 0, 0.768, 1.1, 0.85, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
PWR("Acoustic_input", 1) = New MWNumericArray({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
Dim Control As New MWNumericArray
Control = {0, 4, 0, 1, 0, 0, 0, 0, -0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3}
Dim Res As MWStructArray
Dim ResFields As [String]() = {"AIR_", "Result_", "ErrorCode_"}
Res = New MWStructArray(1, 3, ResFields)
Res("AIR_", 1) = AIR
Res("Result_", 1) = New MWNumericArray
Res("ErrorCode_", 1) = New Double
Try
obj = New myFkt.Class1()
Res = obj.myFkt(AIR, PWR, Control)
Catch
Throw
End Try
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Trying to call the function this way ends up with this error:
... MWMCR::EvaluateFunction error ... Field reference for multiple structure elements that is followed by more reference blocks is an error. Error in => myFkt.m at line 8.
In line 8 would be the first access to a member of the argument AIR.
I appreciate any hint or idea for how to call a DLL in .NET with such arguments? Do I need to use MWStructArray or MWObjectArray or something else to build the arguments properly?
Best regards, Peter Hladky
0 个评论
回答(2 个)
Selva Karna
2017-8-23
Example: code start here :
function out=function_name(x,y)
out=x+y;
////////////////////
then you can command in command window as deploytool * then select .Net library
* then brows your function file
* if u have write more function file link its automatically initialized ,
* then declare Class name and Object
* then generate DLL
*After that you get 3 various folder after u just brows testing dll and use in Visual studio? u can interface with MATLAB Dll
2 个评论
Ken M.
2017-10-25
I have the same problem only I don't get an error but I do only get the first struct back in the object (comparable to your AIR) We only use MWStructArray though.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!