Pass MWNumericArray from c# to MATLAB

My end goal is a test dll with the intent to pass MWStructArray out of C# into MATLAB. Rather than worry about my struct code being flawed, I chose to make similar code for testing MWNumericArray. I am using MATLAB2013b with MCRv82 in windows using VisualStudio2010 but I apear to have the same issue. What do I need to do to my environment to get MATLAB to use the C# code? Below are my notes on the various crashes.
Calling the .Net code from a C# exe works fine
Adding ...\MATLAB Compiler Runtime\v82\bin\win64 and ...\MATLAB Compiler Runtime\v82\runtime\win64 to the system Path was necessary for C# unit test to work.
Earlier versions of the C# exe programatically edited Path but in order to make it easier in MATLAB, the Path was set in regedit and the machine was restarted.
MCR was recently installed without any issues, but the manual Path manipulation implies that something went wrong. (My path has >1024 characters and some routines like setx will only set the fisrt 1024 characters. The manual setup was likely required, but I would be concerned that a required directory is still missing.)
When calling the .Net code from MATLAB there are two groups of exceptions:
1) This is the unhandled series of nested exceptions:
The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' threw an exception.
The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception.
The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.
Trouble initializing libraries required by Builder NE.
2) Debugging from VS2010 uncovers handled exceptions/messages related to the addAssembly call in the matlab script (they seem to be caused by dotnetcli.dll and may be related to the unhandled exceptions):
A) The first handled exception seems to be the most important:
i)
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: Could not load file or assembly 'System' or one of its dependencies. The system cannot find the file specified.
ii) the above exception has the following fusionlog:
=== Pre-bind state information ===
LOG: DisplayName = System
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Program Files/MATLAB/R2013b/bin/win64/
LOG: Initial PrivatePath = NULL
Calling assembly : dotnetcli, Version=1.0.4965.38847, Culture=neutral, PublicKeyToken=da1231a838c93da4.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Program Files/MATLAB/R2013b/bin/win64/System.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/MATLAB/R2013b/bin/win64/System/System.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/MATLAB/R2013b/bin/win64/System.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/MATLAB/R2013b/bin/win64/System/System.EXE.
iii) it is posible to add a copy of system.dll to the malab bin directory and this exception does not get thrown
however the copy is NOT loaded (Systems.dll seems to be loaded from gac and the dll can be deleted durring the runtime)
2)
A first chance exception of type 'System.Exception' occurred in dotnetcli.dll
Additional information: Could not load the type 'System.Collections.Generic.IEnumerable<System*Reflection*TypeInfo>'.
3)
A first chance exception of type 'System.Exception' occurred in dotnetcli.dll
Additional information: Could not load the type 'System.Collections.Generic.IEnumerable<System*Type>'.
4)
A first chance exception of type 'System.Exception' occurred in dotnetcli.dll
Additional information: Could not load the type 'System.Collections.Generic.IEnumerable<System*Reflection*CustomAttributeData>'.
5)
A first chance exception of type 'System.Exception' occurred in dotnetcli.dll
Additional information: Could not load the type 'System.Collections.Generic.IEnumerable<System*Reflection*Module>'.
6)
A first chance exception of type 'System.Exception' occurred in dotnetcli.dll
Additional information: Could not load the type 'System.Collections.Generic.IList<System*Reflection*CustomAttributeData>'.
relevent C# code:
using System;
using MathWorks.MATLAB.NET.Arrays;//gac reference makes me feal uneasy
namespace BasicLib
{
public class Helper
{
public Helper()
{
string p = Environment.GetEnvironmentVariable("PATH");
//Console.WriteLine(p);//can verify the MCR paths when calling from C#
}
public string dllTest()
{
try
{
MWNumericArray five = 5;
}
catch (Exception e)
{
string msg = "";
while (e != null)
{
msg = msg + "\n" + e.Message;
e = e.InnerException;
}
return msg;
}
return "OK";
}
public MWNumericArray getFive()
{
MWNumericArray five = 5;
return five;
}
}
}
relevent matlab code:
src = [loc '\BasicLib.dll'];
NET.addAssembly(src);
H=BasicLib.Helper
H.dllTest()
H.getFive()
C# testing code:
using System;
using MathWorks.MATLAB.NET.Arrays;//gac reference makes me feal uneasy
using BasicLib;
namespace BasicTests
{
public class Program
{
public static int Main(string[] args)
{
int err=0;
Helper h = new Helper();
if (h.dllTest() != "OK")
err++;
Console.WriteLine(err);
double[,] t = (double[,])h.getFive().ToArray();
Console.WriteLine(t[0,0]);
return err;
}
}
}
sample ouput from C# test (as expected):
0
5

 采纳的回答

Martijn
Martijn 2020-9-30
Bringing MATLAB Compiler SDK components (.NET, Java, Python, C/C++, etc.) back into MATLAB is quite simply not supported. Theoretically this would require a MATLAB Runtime to be instantiated inside MATLAB; both MATLAB as well as the MATLAB Runtime have not been designed for this use-case and there simply are too many conflicts between the two for this to simply work.

5 个评论

So is there a set of conditions to make it work in a difficult manner? (Like exact version matching or constraints on data types...)
Is there a plan to support it in the future? (days/weeks/months/years/never)
MWStructArray and MWNumericArray are meant to be used in a .NET application to pass data to compiled MATLAB functions in MATLAB Compiler SDK .NET Assemblies. These have never been intended to be used to pass data from .NET to a normal running MATLAB session. If you are calling .NET from a normal MATLAB session, there is no need for any MATLAB specific types, your .NET code can simply return .NET structs or objects with public fields and MATLAB can simply work with them directly.
What is it that you are actually trying to achieve here? I do not understand your use-case, if you simply want to pass structures from .NET to MATLAB, all you have to do is simply pass .NET structures from .NET to MATLAB, there is no need to do anything with MWStructArray. If you are actually trying to work with some MATLAB Compiler SDK .NET Assembly, why are you bringing this back into MATLAB and why are you not calling it from a .NET Application (which is what they are intended for)?
MATLAB is used extensively for a variety of prototype applications. In the productization process various prototype interfaces from C# to the older MATLAB prototype were maintained by using the MCR interfaces. As more functionality migrates to the C# productized code there is still a need to provide data to MATLAB environments.
The built-in .Net support in matlab requires wrapper code for things like dictionaries of dictionaries and arbitrairy classes/data types. When the primary objective is the C# product code, my MATLAB users suffer because the paths to get data into MATLAB needs to go through extra code layers (C# and MATLAB).
I want to go from C# to matlab using the (already developed) interfaces that were taking me from C# to MCR. The existing MCR interfaces look remarkably similar to the interfaces that I recall from writing mex files. I want MATLAB to be able to convert MCR objects (at least for paired dll versions of MCR and MATLAB) similar to how MATLAB can convert basic .NET objects.
I take the "simply work" statement in the first reply as there is a way for it to work but it is difficult. I take the "not supported" statment to be specific to existing versions. I therefor ask again:
Is there a set of conditions to make C#-MCR-MATLAB work today?
Is there a plan to support C#-MCR-MATLAB in the future?
A yes for either means I should make plans to include deeper more robust suport for my MATLAB users.
Maybe "simply work" was not the right term, maybe I should have said "happen to work". What I meant to say is: it was not designed for this and it also does not just happen to work even if it was not designed for this. And no this is not version dependent, you really cannot instantiate any MCR version inside any MATLAB version.
From your latest description it does not sound like that is what you really need though anyway. Needing the MCR is a requirement of working with the MWArray Assembly which you were trying to work with; but do you really have to work with MWArray here?
You seem to be looking for a way to pass data from C# to MATLAB and not from C# to a MATLAB Compiler SDK .NET Assembly, so this is a completely different use-case for which a completely different possible future enhancement could be considered. Maybe have a different Assembly similar to MWArray but without a dependency on an MCR?
Please submit an actual support request with MathWorks Technical Support:
About your use-case such that we can properly capture this for a possible future enhacement.
Really need is debatable. C# to MATLAB is a primary use case, but there are secondary use cases where C# needs to still access MATLAB Compiler SDK .NET Assembly. I have existing code for the latter that I want to use with (the above mentioned non-existent) C# to MATLAB interfaces. From a certain perspective, I would expect to look similar to a mex file...
At any rate, without such a feature already being viable or in the works, I need to make plans for the moment.The other department has the support plan (since they use matlab much more). I will leave it to them to make such a request when maintaining the extra layers for converting via the supported .net objects becomes impracticle.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Deploy to .NET Applications Using MWArray API 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by