I have a .NET DLL I'm loading into MATLAB R2016B through the asm = NET.addAssembly('C:\My.dll')
It is fairly easy to create a .NET list of doubles using netDouble = NET.createGeneric('System.Collections.Generic.List',{'System.Double'},1)
which MATLAB shows as 1x1 List<System*Double>
, but there is no tutorial out there that shows how to create a .NET List<List<double>>
structure. MATLAB can't access the code I need to run as a result - it shows the error as: Value must be 'System.Collections.Generic.List<System*Collections*Generic*List<System*Double>>'.
Anyone out there know how to create the above structure? Much appreciated.
Actually MATHWORKS tech support got back to me with a solution that works. Since I don't see it documented anywhere, I'm posting this example in case someone else runs into this issue:
genCls = NET.GenericClass('System.Collections.Generic.List','System.Double');
obj = NET.createGeneric('System.Collections.Generic.List',{genCls},5);
And how they explained it: In this method, we first create a generic class as List and this class is provided as an argument to the 'NET.createGeneric' method hence the created list is of type List<List<double>>
Then to populate it... here's a function I wrote to take the Matlab array and convert to the proper type all rolled into 1:
function [NETobj] = ML_NET_ListListDouble(MLarray)
genCls = NET.GenericClass('System.Collections.Generic.List','System.Double');
NETobj = NET.createGeneric('System.Collections.Generic.List',{genCls},size(MLarray,1));
for i=1:size(MLarray,1)
obj1 = NET.createGeneric('System.Collections.Generic.List',{'System.Double'},size(MLarray,2));
AddRange(obj1, NET.convertArray(MLarray(i,:))) %insert the current MATLAB row into the NET structure
Add(NETobj, obj1)
end
end
That function hopefully comes in useful for someone else - a brainless conversion from a MATLAB array to a .NET <List<List<double>>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments