Mex function out plhs[0] is all zeros
Show older comments
Hello, I am fairly new to programming mex function and new to C++. I am trying to write a mex function that accepts data from matlab and processes it and then outputs the processed data. I have copied pasted my c++ code. my plhs code is giving me all 0s and i dont know why that is happening. the result data (odata) looks correct (i know because I used mexprintf to print it out and compare it to the h_raw data which is the input.
#include <iostream>
#include "mex.h"
short* matData(short *h_raw, int nrows,int ncols) {
int dim_x = nrows;
int dim_y = ncols;
int total_val = dim_x * dim_y;
short* odata;
odata = new short[total_val];
for (int i = 0; i < total_val; i++) {
int idx = i;
odata[idx] = h_raw[i];
}
//short odata = h_raw;
return odata;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
short *h_raw = (short *)mxGetData(prhs[0]);
int nrows = 1024;
int ncols = 128;
int nframes = 1;
int ndim = 3;
const mwSize dims[] = { nrows, ncols, nframes };
plhs[0] = mxCreateNumericArray(ndim, dims, mxUINT16_CLASS, mxREAL);
short* res = (short *)mxGetData(plhs[0]);
short *result = matData( h_raw, nrows, ncols);
res = result;
return;
//plhs[0] = *result;
//short t1 = *&result[0];
//mwSize NElem = mxGetNumberOfElements(prhs[0]);
//int size = sizeof(result) / sizeof(result[0]);
//mexPrintf("now h raw %d \n", h_raw[100]);
//mexPrintf("%d \n", h_raw[10]);
//plhs[0] = result;
}
Accepted Answer
More Answers (0)
Categories
Find more on C Shared Library Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!