Ricibob
Ricibob

Reputation: 7705

MWArray to C# array without jumping through hoops

I'm using MATLAB Builder NE to run some MATLAB functions from C#.

I have a MATLAB 1 row, n column vector of doubles returned from my wrapped dll as an MWArray with dimensions 1, n.

What's the easiest way to get a C# double[] array from this? Whatever I do I just seem to get more MWArrays.

Upvotes: 2

Views: 3777

Answers (2)

honzakuzel1989
honzakuzel1989

Reputation: 2520

If you use compatible version of MWArray than you can try nuget package with MWArray extensions which I created.

Conversion to array of type 'T' is simply done by generic method ToArray<T>. In your case double[] result = aMW.ToArray<double>().

Upvotes: 0

chappjc
chappjc

Reputation: 30579

It depends what you consider jumping through hoops. I think you need to cast to one of MWNumericArray, MWLogicalArray, or MWCharArray and use the ToArray method as demonstrated here. Say you have a MWArray called aMW:

double[,] ad = (double[,]) ((MWNumericArray) aMW).ToArray(MWArrayComponent.Real);

MWArray class documentation.

As noted by Patrick, use ToVector() instead of ToArray() to get a vector instead of a 2d array.

Upvotes: 4

Related Questions