Calling matlab from C# won't work in unit tests
3 views (last 30 days)
Show older comments
I have a simple Builder NE project (.NET) with just one function. All it does it take in one parameter
function [ ] = Test_PassDotNetObject( obj )
end
I can call the matlab function no problem from a regular .net application, however it won't work from inside an MSTest unit test.
[TestMethod]
public void PassDotNetObject_SimpleTest()
{
MWObjectArray mwoa;
var simpleMatlab = new Simple.SimpleMatlab();
// pass a struct...
DateTime t = DateTime.Now;
mwoa = new MWObjectArray(t);
simpleMatlab.Test_PassDotNetObject(mwoa);
// pass an object...
String s = "Hello";
mwoa = new MWObjectArray(s);
simpleMatlab.Test_PassDotNetObject(mwoa);
// pass a serializable object...
var simpleObject = new PGL.IP.Modules.Waveforms.Classes.Simple();
mwoa = new MWObjectArray(simpleObject);
simpleMatlab.Test_PassDotNetObject(mwoa);
// pass a non-serializable object...
var simpleNonSerializableObject = new PGL.IP.Modules.Waveforms.Classes.SimpleNonSerializable();
mwoa = new MWObjectArray(simpleNonSerializableObject);
simpleMatlab.Test_PassDotNetObject(mwoa);
}
The first problem I had with the unit tests was that the built matlab dll and the mwarray.dll could not be found. I solved this by adding an app.config telling .NET to look in extra places for these dlls...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="x64;Modules\Waveforms"/>
</assemblyBinding>
</runtime>
</configuration>
The second problem I have, which I can't fix, is that I get the following exception when passing my Simple class to matlab (Just the class I have created is problematic, passing a .NET DateTime is ok. Passing a .NET String is ok). The assembly I have defined the class Simple in, is called UserProgram. The exception is...
"Error marshalling .NET object.\n 'Message: Unable to find assembly 'UserProgram, Version=4.3.0.0, Culture=neutral, PublicKeyToken=35acdb8460030dc2'.\nSource: mscorlib\nHelpLink: '"
So, passing the class Simple from the assembly UserProgram into matlab causes this error. I tried copying the userprogram.dll into lots of other directories to try and get matlab to find it but no luck.
Another weird issue is that passing a non-serializable type into matlab works fine in a regular application but passing a non-serializable class in an MSTest method causes an exception saying that the class Simple is not serializable. I got around that by simple making class Simple serializable but it's odd that I only need to do that in the test mentod, not the regular application.
2 Comments
Answers (0)
See Also
Categories
Find more on Call MATLAB from .NET 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!