MATLAB: Pass MWNumericArray from c# to MATLAB

from c# to matlabmwnumericarraynet

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

Best Answer

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.