Pass MWNumericArray from c# DLL to MATLAB function

5 views (last 30 days)
I have some c# code and compiled them to x64 DLL to be used in MATLAB 2017a. The MWArray.dll I am using is the same version as 2017a. I am using .NET framework 4.6.1.
Here are my c# code:
using MathWorks.MATLAB.NET.Arrays;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MATLABarrayTest
{
public class Data
{
public static int[] GetData1()
{
return new int[] { 555, 666 };
}
public static MWNumericArray GetData2()
{
return new int[] { 555, 666 };
}
}
}
Here are my MATLAB code dotnetdlltest.m:
clear
clc
dllpath = 'C:\Path\to\MATLABarrayTest\bin\x64\Debug\MATLABarrayTest.dll';
asmInfo = NET.addAssembly(dllpath);
data = MATLABarrayTest.Data.GetData1;
This works fine and returns a 1X1 Int32[] data object and I can access the data like:
>> data(2)
ans =
int32
666
However, if I call the GetData2() function as:
clear
clc
dllpath = 'C:\Path\to\MATLABarrayTest\bin\x64\Debug\MATLABarrayTest.dll';
asmInfo = NET.addAssembly(dllpath);
data = MATLABarrayTest.Data.GetData2;
I get the following error message:
Error using dotnetdlltest (line 5)
Message: The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWNumericArray' threw an exception.
Source: MWArray
HelpLink:
I am wondering why the return type MWNumericArray would throw such an error message?
When GetData1() return an object, I was not able to view the data in this object as a table in MATLAB like an array, so I am trying to figure out a way to view it as an array. Is that something possible to do?
Thank you very much for the help!

Answers (0)

Categories

Find more on Java Package Integration in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!