1

The problem is extracting double arrays in a multidimensional object array which itself is kept in a one-dimensional object array that is contained in a variable of object type.

I am trying to use Matlab API in a C# program. The Matlab script returns values in Matlab's matrix form which is then returned to the C# program as an object variable described as above.

An example of the returned data is as follows. The returned variable is called result which contains a one-dimensional object array. The one-dimensional object array contains a 1x4 object array and each of the objects in the 1x4 array contains a double array.

-       result  {object[1]}
  -     [0] {object[1, 4]}
    +       [0, 0]  {double[500, 6]}
    +       [0, 1]  {double[500, 6]}
    +       [0, 2]  {double[500, 6]}
    +       [0, 3]  {double[500, 6]}

How do I extract these double arrays?


Update

To get you a clearer picture of the variable in question, you can run the following code.

object result;
var resultSub1 = new object[1];

double[,] data1 = {{0,0},{0,0}};
double[,] data2 = {{0,0},{0,0}};
double[,] data3 = {{0,0},{0,0}};
double[,] data4 = {{0,0},{0,0}};
object[,] resultSub2 = { { data1, data2, data3, data4 } };

resultSub1[0] = resultSub2;
result = resultSub1;

How do I extract data1, data2, data3 and data4?

3
  • Please be more specific. You show a result value that looks like it's a one-dimensional, single-element array containing a two-dimensional array, with length of the first rank of just 1, a length of the second rank of 4. But even if one assumes that's what you mean, it's not clear at all what it is you want to convert that to. Commented Dec 23, 2014 at 18:04
  • Sorry about my English. @PeterDuniho your assumption is correct. The result is a single-element array containing a two-dimensional array which further contains four two-dimensional arrays. I would like to get those four double[500,6] and store them separately in four new double arrays. The trouble is I define the result as an object rather than an array, but the mLab.Feval method returns an array[object]. I couldn't even get the object[1] out of result{object[1]}. Commented Jan 7, 2015 at 15:04
  • So for example: object result_parent; object[] result_sub1 = new object[1]; double[,] data1 = {{0,0},{0,0}}; double[,] data2 = {{0,0},{0,0}}; double[,] data3 = {{0,0},{0,0}}; double[,] data4 = {{0,0},{0,0}}; object[,] result_sub2 = { { data1, data2, data3, data4 } }; result_sub1[0] = result_sub2; result_parent = result_sub1; How do I call data1, data2, data3 and data4? Commented Jan 7, 2015 at 15:10

2 Answers 2

1

Based on your clarification, I believe this is the code you're looking for:

double[,] data1, data2, data3, data4;

object[] resultArray = (object[]) result;
object[,] dataArrays = (object[,]) resultArray[0];

data1 = (double[,]) dataArrays[0, 0];
data2 = (double[,]) dataArrays[0, 1];
data3 = (double[,]) dataArrays[0, 2];
data4 = (double[,]) dataArrays[0, 3];

It's just a matter of casting appropriately at each level of the data structure. First, cast to the single-dimensional, single-element array type that the result value actually is. Then, retrieve that array's single element, casting it to the two-dimensional, four-element array type that it actually is. Finally, retrieve each of the four elements, casting each to the two-dimensional array of double values that it actually is.

Sign up to request clarification or add additional context in comments.

1 Comment

This is brilliant! Your solution is simple yet works very well. Thank you @Peter for your solution and explanation.
0

Is there a way to get those double arrays inside this object result.

var subArrays = result[0];

The above would create subArrays which has 4 indexes (the double arrays).

If you wanted to combine all the double arrays into a single array (i.e. combine the 4 separate arrays) then this should work:

var allValues = new List<double[][]>();
foreach var subArray in subArrays
{
    allValues.AddRange(subArray);
}
var allValuesArray = allValues.ToArray();

2 Comments

Thanks Jason. It's not possible to call result[0], because the variable result was defined as object result. Sorry I just realised that I didn't explain my questions clearly. Please see the edits I made to the main post. Thanks!
@Anthony - In light of this, my answer really isn't applicable. I will delete it shortly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.