mxCreateSparse returning an empty matrix
Show older comments
Hello I want to create a sparse matrix in c/mex and use it in matlab but I have a problem with mxCreateSparse function. I wrote the below code in c and called it from matlab. But it is creating a 4x2 zeros sparse. I don't know why it is happening. And, When I tried to open that empty sparse matrix, Matlab crashed immediately. Please help me to identify the error. Thanks in advance.
#include <math.h>
#include <stdlib.h>
#include "mex.h"
#include "matrix.h"
#include <stdio.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
double Ap[4] = {5.8, 6.2, 5.9, 6.1}, *cAp;
int Ai[4] = {0, 2, 1, 3}, *cAi ;
int Ax[3] = {0, 2, 4}, *cAx ;
mxArray *pargout;
pargout = mxCreateSparse (4, 2, 4, mxREAL) ;
cAp = (double *)mxGetPr(pargout);
memcpy(cAp, Ap, 4*sizeof(double));
cAi = (int *)mxGetIr(pargout);
memcpy(cAi, Ai, 4*sizeof(int));
cAx = (int *)mxGetJc (pargout) ;
memcpy(cAx, Ax, 4*sizeof(int));
plhs[0] = pargout;
}
Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) 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!