I am using MEX to interface my C++ code with MATLAB. My C++ code requires the output be of type vector . Since I am new to C++ I am extremely confused with how the pointers work. I will take an input array from MATLAB
int *data_array_ptr
data_array_ptr=(int *)mxGetData(prhs[0]);
a = mxGetM(prhs[0]);
int int_data[a];
copy(*data_array_ptr, *data_array_ptr+ a, int_data);
Now, int_data is supposed to have all the data that is stored at the location of data_array_ptr... Does it do this?
Then,
double *data_out_ptr;
plhs[0]= mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
data_out_ptr= mxGetPr(plhs[0]);
len6=mxGetM(plhs[0]);
vector<double> data_out_data(*data_out_ptr,*data_out_ptr+len6);
This should put the contents of the empty output matrix into a vector named data_out_data. Does it do this?
Then, I want to pass both data_out_data and int_data to a c++ function. However, I want to pass data_out_data as a pointer so that the c++ function will fill the vector with data and then when the function finishes, the MEX function will see the now filled vector and be able to convert it back to an array of doubles that can fill plhs[0].
So, something like
mexFunction(plhs[],prhs[]){
int *data_array_ptr
data_array_ptr=(int *)mxGetData(prhs[0]);
a = mxGetM(prhs[0]);
int int_data[a];
copy(*data_array_ptr, *data_array_ptr+ a, int_data);
double *data_out_ptr;
plhs[0]= mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
data_out_ptr= mxGetPr(plhs[0]);
len6=mxGetM(plhs[0]);
vector<double> data_out_data(*data_out_ptr,*data_out_ptr+len6);
foo(int_data, *data_out_data)
copy(data_out_data.begin(), data_out_data.end(), data_out_ptr);
}
and on the return of foo, data_out_data will be filled. My function has no return arguments an data_out_data must be of type vector. How do I pass the vector to foo so that foo can edit the data?
Thanks!!