MATLAB: Passing object array from C# to MATLAB

MATLAB Compilermcr

I have a program where I have to basically send a database to from C# code to MATLAB. If I do this using the COM interface, then I just send a giant object[,] array and it works fine. It sends it pretty much instantly. If I am using the MCR with a compiled .dll, then I have to convert this data to a giant MWCellArray. This table is pretty big (65,000 x 365). Converting this to the MWCellArray takes too long. I am using this because the type of data in each cell is not consistent.
Is there an alternative to using a MWCellArray that would allow me to send a large object array with arbitrary data from C# to the MATLAB Runtime?
I am using the compiled runtime for R2017a.

Best Answer

So I think I found a solution but I don't understand why it works. If anyone knows, please let me know.
My old code was:
MWCellArray output = new MWCellArray(rows, columns);
for (int i =0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
output[i+1, j+1] = WrapObject<T>(input[i, j]);
}
}
return output;
In this code, T might be MWCellArray and an empty MWCellArray would be returned if the input value is null. I think this was a big part of the problem.
Changing WrapObject to return null and using this to this code speeds it up a little:
MWCellArray output = new MWCellArray(rows, columns);
for (int i =0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
MWArray value = WrapObject<T>(input[i, j]);
if (value != null)
output[i+1, j+1] = value;
}
}
return output;
But this is what actually works and takes probably 5% as long as the original code:
MWArray[,] cache = new MWArray[rows, columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
MWArray value = WrapObject<T>(input[i, j]);
if (value != null)
cache[i, j] = value;
}
}
MWCellArray output = new MWCellArray(rows, columns);
for (int i =0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
MWArray value = cache[i, j];
if (value != null)
output[i+1, j+1] = value;
}
}
Why does building directly on the MWCellArray slow everything down?