Passing matrix into C DLL

Hi,
I'm calling a C DLL I'll call mylib from Matlab - I'm also developing on the C side, so I'm able to change anything on the DLL side to make this work.
Here's a simplified version of the header file for my DLL:
struct OD_INPUT {
char *Name;
.
.
.
.
int numObs;
double **Meas;
double **Sigma;
};
MYLIB_API int od_call(struct OD_INPUT input, double Mat1[][4], double Mat2[][4], char *argv[]);
I tried following the advice I found here, which suggested using a format such as Mat1[][4]. So I decided to approach the problem this way:
  1. Pass in an OD_INPUT struct with empty Meas and Sigma
  2. Pass in the intended contents of Meas and Sigma via the function arguments Mat1 and Mat2
  3. Inside od_call, write Mat1 and Mat2 to input.Meas and input.Sigma using the code snippet below:
int od_call(struct OD_INPUT input, double Mat1[][4], double Mat2[][4], char *argv[])
{
int i, j;
input.Meas = (double **)malloc(input.numObs* sizeof(double*));
for (i = 0; i < input.numObs; i++)
input.Meas[i] = (double *)malloc(4 * sizeof(double));
for (i = 0; i < input.numObs; i++)
for (j = 0; j < 4; j++)
{
input.Meas[i][j] = Mat1[i][j];
}
input.Sigma = (double **)malloc(input.numObs * sizeof(double*));
for (i = 0; i < input.numObs; i++)
input.Sigma[i] = (double *)malloc(4 * sizeof(double));
for (i = 0; i < input.numObs; i++)
for (j = 0; j < 4; j++)
input.Sigma[i][j] = Mat2[i][j];
The issue is when I call the DLL from Matlab using
calllib('castsosi', 'od_call', odt, Mat1, Mat2, argv)
Where the variable types are:
odt - OD_INPUT
Mat1- 13x4 Double
Mat2 - 13x4 Double
argv - 1x3 cellll
The matrix get read incorrectly on the C side and Matlab crashes a bit into the reading. I added print statements to see what the values being written to input.Meas are all large integers that don't at all correspond to the actual input.
I've also tried passing in Mat1 and Mat2 as a doubleptr and a doubleptrptr, but neither has worked. This is actually the closest I've gotten to this working out of all the variations I've tried, but it obviously still isn't functional. Any advice is appreciated

Answers (0)

Asked:

on 26 Aug 2020

Edited:

on 26 Aug 2020

Community Treasure Hunt

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

Start Hunting!