MATLAB Answers

How to pass arguments to a deployed DLL within .NET, if the arguments are of type struct and not straight numerical arrays?

10 views (last 30 days)
Peter Hladky
Peter Hladky on 23 Aug 2017
Edited: Ken M. on 25 Oct 2017
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 Comments

Sign in to comment.

Answers (2)

Selva Karna
Selva Karna on 23 Aug 2017
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 Comments

Peter Hladky
Peter Hladky on 24 Aug 2017
Thx, but all this I know and this was not my question. My question is more specific about the type of varibles (arguments) that are passed to the function in my given function. For example: AIR Is not a simple numerical array but a structure of arrays. There is AIR.Vdot, AIR.T ... and so on. I need to know how can I build the proper type / structure in .NET to pass all arguments to the DLL. In my code I am using MWStructArray and MWNumericArray but I am getting error when calling the DLL from .NET. Just look more in detail to the code I supplied in my question.
Ken M.
Ken M. on 25 Oct 2017
Try not too use MWNumericArray, in our code we use a for each to fill-up the MWStructArray. But like I said then we only get the first struct out of the function, don't know how to get the others. (we pass a combination of doubles and structs to the functions)

Sign in to comment.


Ken M.
Ken M. on 25 Oct 2017
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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!