C and MATLAB: Create empty array in C calls Matlab function to fill it ends in error










0















This is the problem:



I have a C code that creates an array nRows x 2, called InitialArray filled with random numbers. Then I split that array into two parts, that is the first column is called xProcess and the second column of the array is yProcess.
The goal is to pass these variables I created (xProcess and yProcess) to Matlab and there a Matlab function based on probabilities rules will put NaN in some of the elements of xProcess and yProcess, and will fill empty arrays called xC and yC with the values of xProcess and yProcess before turning to NaN.
Finally, the C code gets all four arrays back again, perform some operations on them and send them again to Matlab. This process is repeated for a number of time steps.



Here is a piece of the code:



int main ( void )

Engine *ep;
if (!(ep = engOpen("")))

fprintf(stderr, "nCan't start MATLAB enginen");
return EXIT_FAILURE;


mxArray *xProcess = NULL;
mxArray *yProcess = NULL;
mxArray *xC = NULL;
mxArray *yC = NULL;

int timeStep;
int numberProcess = 4e6;
int numberC = 0;


So far I created the mxArray, and the number of random numbers that will be stored in xProcess and yProcess. Now comes the loop.



for (timeStep=0 ; timeStep<numberTimeSteps ; timeStep++)

double *arrayTempxProcess;
arrayTempxProcess = (double *)calloc(numberProcess, sizeof(double));
double *arrayTempyProcess;
arrayTempyProcess = (double *)calloc(numberProcess, sizeof(double));

double *arrayTempxC;
arrayTempxC=(double *)calloc(numberC, sizeof(double));
double *arrayTempyC;
arrayTempyC=(double *)calloc(numberC, sizeof(double));

xProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);
yProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);

xC = mxCreateDoubleMatrix(numberC, 1, mxREAL);
yC = mxCreateDoubleMatrix(numberC, 1, mxREAL);

memcpy((void *)mxGetPr(yProcess), (void *)arrayTempyProcess, numberProcess*sizeof(double));
memcpy((void *)mxGetPr(xProcess), (void *)arrayTempxProcess, numberProcess*sizeof(double));


As you see xC and yC I create them as empty. I believe this is equivalent to xC = and yC = in Matlab. And the arrayTempyProcess and arrayTempxProcess are temporal arrays that each of them gets a column of InitialArray.



if(timeStep>0)
memcpy((void *)mxGetPr(yC), (void *)arrayTempyC,(numberC)*sizeof(double));
memcpy((void *)mxGetPr(xC), (void *)arrayTempxC, (numberC)*sizeof(double));




Now I send them to Matlab



engPutVariable(ep, "xProcess", xProcess);
engPutVariable(ep, "yProcess", yProcess);
engPutVariable(ep, "xC", xC);
engPutVariable(ep, "yC", yC);


free(arrayTempxProcess);
free(arrayTempyProcess);
free(arrayTempxC);
free(arrayTempyC);


Now the Matlab function puts NaN in xProcess and yProcess, and fills xC and yC.



printf("Entering MATLAB n");
engEvalString(ep, "[xProcess,yProcess,xC, yC] = my_matlab_function(xProcess,yProcess,xC, yC);");
printf("Leaving MATLAB n");


Now comes trouble... Now I am trying to get the results... but the function mxGetM(xC) produces a segmentation fault in the first time step that I don't understand.



xC = engGetVariable(ep,"xC"); 
int mRowsC = mxGetM(xC);


double *XC = NULL;
XC = (double *)malloc (mRowsC*sizeof(double) );
memcpy(XC, mxGetData(xC),mRowsC*sizeof(double));


yC = engGetVariable(ep,"yC");
double *YC = NULL;
YC = (double *)malloc (mmy*sizeof(double) );
memcpy(YC, mxGetData(yC),mmy*sizeof(double));


xProcess = engGetVariable(ep,"xProcess");
int mRowsProcess = mxGetM(xProcess);


double *XPROCESS = NULL;
XPROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(XPROCESS, mxGetData(xPROCESS),mRowsProcess*sizeof(double));


yProcess = engGetVariable(ep,"yProcess");

double *YPROCESS = NULL;
PROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(YPROCESS, mxGetData(yProcess),mRowsProcess*sizeof(double));



numberC = mRowsC;

mxDestroyArray(xProcess);
mxDestroyArray(yProcess);
mxDestroyArray(xC);
mxDestroyArray(yC);
free(XPROCESS);
free(YPROCESS);
free(XC);
free(YC);




I have tried to reduce the number of elements in xProcess and yProcess but I get the segmentation fault at some point later than the first time step.
The weird thing about this, is that I also tried to save the results in Matlab after printf("Leaving MATLAB n"); but it didn't work out. It saved nothing.



I'm new coupling C and Matlab, so my code might be entirely wrong, but is there any limits in which C cannot be coupled with Matlab functions?



Any help is appreciated. Thanks!










share|improve this question
























  • The question title says that the issue is about creating empty arrays. Have you done any tests that narrows it down to that problem or is it a guess? also, there is a typo memcpy(XC, mxGetData(xC),mRowsProcess*sizeof(double)); that should be memcpy(XPROCESS, mxGetData(xProcess),mRowsProcess*sizeof(double));

    – Brice
    Nov 14 '18 at 13:04












  • I have done tests in order to get which line of the code causes the segmentation fault in C. And I narrowed it down to the mxGetM(xC) line. When I don't fill xC in Matlab I don't get any errors. So my question is more if this is the proper way to fill an empty array in a C code calling a Matlab function.

    – S. Perez
    Nov 14 '18 at 15:07











  • xC is created in C, then passed to the Matlab engine. What gets out of that may be totally unrelated to what gets in (code should still work with separate variables xC_in and xC_out). Actually, you could have a Matlab function that does not take any argument and still returns some variable xC. Have you examined what actually gets in the Matlab function? You could use engEvalString(ep, "save('dump_input.mat','xProcess','yProcess','xC','yC');"); to check what actually gets into the Matlab function (and another one just after to dump the output). Or do that from within the Matlab function.

    – Brice
    Nov 14 '18 at 15:34







  • 1





    engGetVariable will return a NULL pointer in case of failure, which would be a cause for the seg fault with mxGetM. You should test that. The variables should exist in Matlab engine workspace regardless of your Matlab function success or failure, though, since they have the same name as the input variables. That is, as long as the calls to engPutVariable succeeded (i.e. returned 0). You should test that too. Replace engPutVariable(ep,"xC",xC) with if(engPutVariable(ep,"xC",xC)) printf('Error putting xCn');fflush(stdout);

    – Brice
    Nov 14 '18 at 15:46












  • You nailed it! But I still don’t know what is failing. So I saved the input before sending it to Matlab and everything is right. However, saving the output coming from Matlab function saved nothing. Moreover I added a condition if xC is NULL and this condition is met! So clearly what is failing is the line ‘engEvalString(ep,”[xProcess,yProcess,xC,yC] = my_matlab_function(xProcess,yProcess,xC,yC);”);’. But I don’t get it. How is it possible to fail? If I have an error in my_matlab_function this will appear on screen as an error in Matlab. Any guess?

    – S. Perez
    Nov 14 '18 at 16:39
















0















This is the problem:



I have a C code that creates an array nRows x 2, called InitialArray filled with random numbers. Then I split that array into two parts, that is the first column is called xProcess and the second column of the array is yProcess.
The goal is to pass these variables I created (xProcess and yProcess) to Matlab and there a Matlab function based on probabilities rules will put NaN in some of the elements of xProcess and yProcess, and will fill empty arrays called xC and yC with the values of xProcess and yProcess before turning to NaN.
Finally, the C code gets all four arrays back again, perform some operations on them and send them again to Matlab. This process is repeated for a number of time steps.



Here is a piece of the code:



int main ( void )

Engine *ep;
if (!(ep = engOpen("")))

fprintf(stderr, "nCan't start MATLAB enginen");
return EXIT_FAILURE;


mxArray *xProcess = NULL;
mxArray *yProcess = NULL;
mxArray *xC = NULL;
mxArray *yC = NULL;

int timeStep;
int numberProcess = 4e6;
int numberC = 0;


So far I created the mxArray, and the number of random numbers that will be stored in xProcess and yProcess. Now comes the loop.



for (timeStep=0 ; timeStep<numberTimeSteps ; timeStep++)

double *arrayTempxProcess;
arrayTempxProcess = (double *)calloc(numberProcess, sizeof(double));
double *arrayTempyProcess;
arrayTempyProcess = (double *)calloc(numberProcess, sizeof(double));

double *arrayTempxC;
arrayTempxC=(double *)calloc(numberC, sizeof(double));
double *arrayTempyC;
arrayTempyC=(double *)calloc(numberC, sizeof(double));

xProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);
yProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);

xC = mxCreateDoubleMatrix(numberC, 1, mxREAL);
yC = mxCreateDoubleMatrix(numberC, 1, mxREAL);

memcpy((void *)mxGetPr(yProcess), (void *)arrayTempyProcess, numberProcess*sizeof(double));
memcpy((void *)mxGetPr(xProcess), (void *)arrayTempxProcess, numberProcess*sizeof(double));


As you see xC and yC I create them as empty. I believe this is equivalent to xC = and yC = in Matlab. And the arrayTempyProcess and arrayTempxProcess are temporal arrays that each of them gets a column of InitialArray.



if(timeStep>0)
memcpy((void *)mxGetPr(yC), (void *)arrayTempyC,(numberC)*sizeof(double));
memcpy((void *)mxGetPr(xC), (void *)arrayTempxC, (numberC)*sizeof(double));




Now I send them to Matlab



engPutVariable(ep, "xProcess", xProcess);
engPutVariable(ep, "yProcess", yProcess);
engPutVariable(ep, "xC", xC);
engPutVariable(ep, "yC", yC);


free(arrayTempxProcess);
free(arrayTempyProcess);
free(arrayTempxC);
free(arrayTempyC);


Now the Matlab function puts NaN in xProcess and yProcess, and fills xC and yC.



printf("Entering MATLAB n");
engEvalString(ep, "[xProcess,yProcess,xC, yC] = my_matlab_function(xProcess,yProcess,xC, yC);");
printf("Leaving MATLAB n");


Now comes trouble... Now I am trying to get the results... but the function mxGetM(xC) produces a segmentation fault in the first time step that I don't understand.



xC = engGetVariable(ep,"xC"); 
int mRowsC = mxGetM(xC);


double *XC = NULL;
XC = (double *)malloc (mRowsC*sizeof(double) );
memcpy(XC, mxGetData(xC),mRowsC*sizeof(double));


yC = engGetVariable(ep,"yC");
double *YC = NULL;
YC = (double *)malloc (mmy*sizeof(double) );
memcpy(YC, mxGetData(yC),mmy*sizeof(double));


xProcess = engGetVariable(ep,"xProcess");
int mRowsProcess = mxGetM(xProcess);


double *XPROCESS = NULL;
XPROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(XPROCESS, mxGetData(xPROCESS),mRowsProcess*sizeof(double));


yProcess = engGetVariable(ep,"yProcess");

double *YPROCESS = NULL;
PROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(YPROCESS, mxGetData(yProcess),mRowsProcess*sizeof(double));



numberC = mRowsC;

mxDestroyArray(xProcess);
mxDestroyArray(yProcess);
mxDestroyArray(xC);
mxDestroyArray(yC);
free(XPROCESS);
free(YPROCESS);
free(XC);
free(YC);




I have tried to reduce the number of elements in xProcess and yProcess but I get the segmentation fault at some point later than the first time step.
The weird thing about this, is that I also tried to save the results in Matlab after printf("Leaving MATLAB n"); but it didn't work out. It saved nothing.



I'm new coupling C and Matlab, so my code might be entirely wrong, but is there any limits in which C cannot be coupled with Matlab functions?



Any help is appreciated. Thanks!










share|improve this question
























  • The question title says that the issue is about creating empty arrays. Have you done any tests that narrows it down to that problem or is it a guess? also, there is a typo memcpy(XC, mxGetData(xC),mRowsProcess*sizeof(double)); that should be memcpy(XPROCESS, mxGetData(xProcess),mRowsProcess*sizeof(double));

    – Brice
    Nov 14 '18 at 13:04












  • I have done tests in order to get which line of the code causes the segmentation fault in C. And I narrowed it down to the mxGetM(xC) line. When I don't fill xC in Matlab I don't get any errors. So my question is more if this is the proper way to fill an empty array in a C code calling a Matlab function.

    – S. Perez
    Nov 14 '18 at 15:07











  • xC is created in C, then passed to the Matlab engine. What gets out of that may be totally unrelated to what gets in (code should still work with separate variables xC_in and xC_out). Actually, you could have a Matlab function that does not take any argument and still returns some variable xC. Have you examined what actually gets in the Matlab function? You could use engEvalString(ep, "save('dump_input.mat','xProcess','yProcess','xC','yC');"); to check what actually gets into the Matlab function (and another one just after to dump the output). Or do that from within the Matlab function.

    – Brice
    Nov 14 '18 at 15:34







  • 1





    engGetVariable will return a NULL pointer in case of failure, which would be a cause for the seg fault with mxGetM. You should test that. The variables should exist in Matlab engine workspace regardless of your Matlab function success or failure, though, since they have the same name as the input variables. That is, as long as the calls to engPutVariable succeeded (i.e. returned 0). You should test that too. Replace engPutVariable(ep,"xC",xC) with if(engPutVariable(ep,"xC",xC)) printf('Error putting xCn');fflush(stdout);

    – Brice
    Nov 14 '18 at 15:46












  • You nailed it! But I still don’t know what is failing. So I saved the input before sending it to Matlab and everything is right. However, saving the output coming from Matlab function saved nothing. Moreover I added a condition if xC is NULL and this condition is met! So clearly what is failing is the line ‘engEvalString(ep,”[xProcess,yProcess,xC,yC] = my_matlab_function(xProcess,yProcess,xC,yC);”);’. But I don’t get it. How is it possible to fail? If I have an error in my_matlab_function this will appear on screen as an error in Matlab. Any guess?

    – S. Perez
    Nov 14 '18 at 16:39














0












0








0








This is the problem:



I have a C code that creates an array nRows x 2, called InitialArray filled with random numbers. Then I split that array into two parts, that is the first column is called xProcess and the second column of the array is yProcess.
The goal is to pass these variables I created (xProcess and yProcess) to Matlab and there a Matlab function based on probabilities rules will put NaN in some of the elements of xProcess and yProcess, and will fill empty arrays called xC and yC with the values of xProcess and yProcess before turning to NaN.
Finally, the C code gets all four arrays back again, perform some operations on them and send them again to Matlab. This process is repeated for a number of time steps.



Here is a piece of the code:



int main ( void )

Engine *ep;
if (!(ep = engOpen("")))

fprintf(stderr, "nCan't start MATLAB enginen");
return EXIT_FAILURE;


mxArray *xProcess = NULL;
mxArray *yProcess = NULL;
mxArray *xC = NULL;
mxArray *yC = NULL;

int timeStep;
int numberProcess = 4e6;
int numberC = 0;


So far I created the mxArray, and the number of random numbers that will be stored in xProcess and yProcess. Now comes the loop.



for (timeStep=0 ; timeStep<numberTimeSteps ; timeStep++)

double *arrayTempxProcess;
arrayTempxProcess = (double *)calloc(numberProcess, sizeof(double));
double *arrayTempyProcess;
arrayTempyProcess = (double *)calloc(numberProcess, sizeof(double));

double *arrayTempxC;
arrayTempxC=(double *)calloc(numberC, sizeof(double));
double *arrayTempyC;
arrayTempyC=(double *)calloc(numberC, sizeof(double));

xProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);
yProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);

xC = mxCreateDoubleMatrix(numberC, 1, mxREAL);
yC = mxCreateDoubleMatrix(numberC, 1, mxREAL);

memcpy((void *)mxGetPr(yProcess), (void *)arrayTempyProcess, numberProcess*sizeof(double));
memcpy((void *)mxGetPr(xProcess), (void *)arrayTempxProcess, numberProcess*sizeof(double));


As you see xC and yC I create them as empty. I believe this is equivalent to xC = and yC = in Matlab. And the arrayTempyProcess and arrayTempxProcess are temporal arrays that each of them gets a column of InitialArray.



if(timeStep>0)
memcpy((void *)mxGetPr(yC), (void *)arrayTempyC,(numberC)*sizeof(double));
memcpy((void *)mxGetPr(xC), (void *)arrayTempxC, (numberC)*sizeof(double));




Now I send them to Matlab



engPutVariable(ep, "xProcess", xProcess);
engPutVariable(ep, "yProcess", yProcess);
engPutVariable(ep, "xC", xC);
engPutVariable(ep, "yC", yC);


free(arrayTempxProcess);
free(arrayTempyProcess);
free(arrayTempxC);
free(arrayTempyC);


Now the Matlab function puts NaN in xProcess and yProcess, and fills xC and yC.



printf("Entering MATLAB n");
engEvalString(ep, "[xProcess,yProcess,xC, yC] = my_matlab_function(xProcess,yProcess,xC, yC);");
printf("Leaving MATLAB n");


Now comes trouble... Now I am trying to get the results... but the function mxGetM(xC) produces a segmentation fault in the first time step that I don't understand.



xC = engGetVariable(ep,"xC"); 
int mRowsC = mxGetM(xC);


double *XC = NULL;
XC = (double *)malloc (mRowsC*sizeof(double) );
memcpy(XC, mxGetData(xC),mRowsC*sizeof(double));


yC = engGetVariable(ep,"yC");
double *YC = NULL;
YC = (double *)malloc (mmy*sizeof(double) );
memcpy(YC, mxGetData(yC),mmy*sizeof(double));


xProcess = engGetVariable(ep,"xProcess");
int mRowsProcess = mxGetM(xProcess);


double *XPROCESS = NULL;
XPROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(XPROCESS, mxGetData(xPROCESS),mRowsProcess*sizeof(double));


yProcess = engGetVariable(ep,"yProcess");

double *YPROCESS = NULL;
PROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(YPROCESS, mxGetData(yProcess),mRowsProcess*sizeof(double));



numberC = mRowsC;

mxDestroyArray(xProcess);
mxDestroyArray(yProcess);
mxDestroyArray(xC);
mxDestroyArray(yC);
free(XPROCESS);
free(YPROCESS);
free(XC);
free(YC);




I have tried to reduce the number of elements in xProcess and yProcess but I get the segmentation fault at some point later than the first time step.
The weird thing about this, is that I also tried to save the results in Matlab after printf("Leaving MATLAB n"); but it didn't work out. It saved nothing.



I'm new coupling C and Matlab, so my code might be entirely wrong, but is there any limits in which C cannot be coupled with Matlab functions?



Any help is appreciated. Thanks!










share|improve this question
















This is the problem:



I have a C code that creates an array nRows x 2, called InitialArray filled with random numbers. Then I split that array into two parts, that is the first column is called xProcess and the second column of the array is yProcess.
The goal is to pass these variables I created (xProcess and yProcess) to Matlab and there a Matlab function based on probabilities rules will put NaN in some of the elements of xProcess and yProcess, and will fill empty arrays called xC and yC with the values of xProcess and yProcess before turning to NaN.
Finally, the C code gets all four arrays back again, perform some operations on them and send them again to Matlab. This process is repeated for a number of time steps.



Here is a piece of the code:



int main ( void )

Engine *ep;
if (!(ep = engOpen("")))

fprintf(stderr, "nCan't start MATLAB enginen");
return EXIT_FAILURE;


mxArray *xProcess = NULL;
mxArray *yProcess = NULL;
mxArray *xC = NULL;
mxArray *yC = NULL;

int timeStep;
int numberProcess = 4e6;
int numberC = 0;


So far I created the mxArray, and the number of random numbers that will be stored in xProcess and yProcess. Now comes the loop.



for (timeStep=0 ; timeStep<numberTimeSteps ; timeStep++)

double *arrayTempxProcess;
arrayTempxProcess = (double *)calloc(numberProcess, sizeof(double));
double *arrayTempyProcess;
arrayTempyProcess = (double *)calloc(numberProcess, sizeof(double));

double *arrayTempxC;
arrayTempxC=(double *)calloc(numberC, sizeof(double));
double *arrayTempyC;
arrayTempyC=(double *)calloc(numberC, sizeof(double));

xProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);
yProcess = mxCreateDoubleMatrix(numberProcess, 1, mxREAL);

xC = mxCreateDoubleMatrix(numberC, 1, mxREAL);
yC = mxCreateDoubleMatrix(numberC, 1, mxREAL);

memcpy((void *)mxGetPr(yProcess), (void *)arrayTempyProcess, numberProcess*sizeof(double));
memcpy((void *)mxGetPr(xProcess), (void *)arrayTempxProcess, numberProcess*sizeof(double));


As you see xC and yC I create them as empty. I believe this is equivalent to xC = and yC = in Matlab. And the arrayTempyProcess and arrayTempxProcess are temporal arrays that each of them gets a column of InitialArray.



if(timeStep>0)
memcpy((void *)mxGetPr(yC), (void *)arrayTempyC,(numberC)*sizeof(double));
memcpy((void *)mxGetPr(xC), (void *)arrayTempxC, (numberC)*sizeof(double));




Now I send them to Matlab



engPutVariable(ep, "xProcess", xProcess);
engPutVariable(ep, "yProcess", yProcess);
engPutVariable(ep, "xC", xC);
engPutVariable(ep, "yC", yC);


free(arrayTempxProcess);
free(arrayTempyProcess);
free(arrayTempxC);
free(arrayTempyC);


Now the Matlab function puts NaN in xProcess and yProcess, and fills xC and yC.



printf("Entering MATLAB n");
engEvalString(ep, "[xProcess,yProcess,xC, yC] = my_matlab_function(xProcess,yProcess,xC, yC);");
printf("Leaving MATLAB n");


Now comes trouble... Now I am trying to get the results... but the function mxGetM(xC) produces a segmentation fault in the first time step that I don't understand.



xC = engGetVariable(ep,"xC"); 
int mRowsC = mxGetM(xC);


double *XC = NULL;
XC = (double *)malloc (mRowsC*sizeof(double) );
memcpy(XC, mxGetData(xC),mRowsC*sizeof(double));


yC = engGetVariable(ep,"yC");
double *YC = NULL;
YC = (double *)malloc (mmy*sizeof(double) );
memcpy(YC, mxGetData(yC),mmy*sizeof(double));


xProcess = engGetVariable(ep,"xProcess");
int mRowsProcess = mxGetM(xProcess);


double *XPROCESS = NULL;
XPROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(XPROCESS, mxGetData(xPROCESS),mRowsProcess*sizeof(double));


yProcess = engGetVariable(ep,"yProcess");

double *YPROCESS = NULL;
PROCESS = (double *)malloc (mRowsProcess*sizeof(double) );
memcpy(YPROCESS, mxGetData(yProcess),mRowsProcess*sizeof(double));



numberC = mRowsC;

mxDestroyArray(xProcess);
mxDestroyArray(yProcess);
mxDestroyArray(xC);
mxDestroyArray(yC);
free(XPROCESS);
free(YPROCESS);
free(XC);
free(YC);




I have tried to reduce the number of elements in xProcess and yProcess but I get the segmentation fault at some point later than the first time step.
The weird thing about this, is that I also tried to save the results in Matlab after printf("Leaving MATLAB n"); but it didn't work out. It saved nothing.



I'm new coupling C and Matlab, so my code might be entirely wrong, but is there any limits in which C cannot be coupled with Matlab functions?



Any help is appreciated. Thanks!







c arrays matlab segmentation-fault






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:36









Ryan Livingston

1,544716




1,544716










asked Nov 14 '18 at 11:34









S. PerezS. Perez

176




176












  • The question title says that the issue is about creating empty arrays. Have you done any tests that narrows it down to that problem or is it a guess? also, there is a typo memcpy(XC, mxGetData(xC),mRowsProcess*sizeof(double)); that should be memcpy(XPROCESS, mxGetData(xProcess),mRowsProcess*sizeof(double));

    – Brice
    Nov 14 '18 at 13:04












  • I have done tests in order to get which line of the code causes the segmentation fault in C. And I narrowed it down to the mxGetM(xC) line. When I don't fill xC in Matlab I don't get any errors. So my question is more if this is the proper way to fill an empty array in a C code calling a Matlab function.

    – S. Perez
    Nov 14 '18 at 15:07











  • xC is created in C, then passed to the Matlab engine. What gets out of that may be totally unrelated to what gets in (code should still work with separate variables xC_in and xC_out). Actually, you could have a Matlab function that does not take any argument and still returns some variable xC. Have you examined what actually gets in the Matlab function? You could use engEvalString(ep, "save('dump_input.mat','xProcess','yProcess','xC','yC');"); to check what actually gets into the Matlab function (and another one just after to dump the output). Or do that from within the Matlab function.

    – Brice
    Nov 14 '18 at 15:34







  • 1





    engGetVariable will return a NULL pointer in case of failure, which would be a cause for the seg fault with mxGetM. You should test that. The variables should exist in Matlab engine workspace regardless of your Matlab function success or failure, though, since they have the same name as the input variables. That is, as long as the calls to engPutVariable succeeded (i.e. returned 0). You should test that too. Replace engPutVariable(ep,"xC",xC) with if(engPutVariable(ep,"xC",xC)) printf('Error putting xCn');fflush(stdout);

    – Brice
    Nov 14 '18 at 15:46












  • You nailed it! But I still don’t know what is failing. So I saved the input before sending it to Matlab and everything is right. However, saving the output coming from Matlab function saved nothing. Moreover I added a condition if xC is NULL and this condition is met! So clearly what is failing is the line ‘engEvalString(ep,”[xProcess,yProcess,xC,yC] = my_matlab_function(xProcess,yProcess,xC,yC);”);’. But I don’t get it. How is it possible to fail? If I have an error in my_matlab_function this will appear on screen as an error in Matlab. Any guess?

    – S. Perez
    Nov 14 '18 at 16:39


















  • The question title says that the issue is about creating empty arrays. Have you done any tests that narrows it down to that problem or is it a guess? also, there is a typo memcpy(XC, mxGetData(xC),mRowsProcess*sizeof(double)); that should be memcpy(XPROCESS, mxGetData(xProcess),mRowsProcess*sizeof(double));

    – Brice
    Nov 14 '18 at 13:04












  • I have done tests in order to get which line of the code causes the segmentation fault in C. And I narrowed it down to the mxGetM(xC) line. When I don't fill xC in Matlab I don't get any errors. So my question is more if this is the proper way to fill an empty array in a C code calling a Matlab function.

    – S. Perez
    Nov 14 '18 at 15:07











  • xC is created in C, then passed to the Matlab engine. What gets out of that may be totally unrelated to what gets in (code should still work with separate variables xC_in and xC_out). Actually, you could have a Matlab function that does not take any argument and still returns some variable xC. Have you examined what actually gets in the Matlab function? You could use engEvalString(ep, "save('dump_input.mat','xProcess','yProcess','xC','yC');"); to check what actually gets into the Matlab function (and another one just after to dump the output). Or do that from within the Matlab function.

    – Brice
    Nov 14 '18 at 15:34







  • 1





    engGetVariable will return a NULL pointer in case of failure, which would be a cause for the seg fault with mxGetM. You should test that. The variables should exist in Matlab engine workspace regardless of your Matlab function success or failure, though, since they have the same name as the input variables. That is, as long as the calls to engPutVariable succeeded (i.e. returned 0). You should test that too. Replace engPutVariable(ep,"xC",xC) with if(engPutVariable(ep,"xC",xC)) printf('Error putting xCn');fflush(stdout);

    – Brice
    Nov 14 '18 at 15:46












  • You nailed it! But I still don’t know what is failing. So I saved the input before sending it to Matlab and everything is right. However, saving the output coming from Matlab function saved nothing. Moreover I added a condition if xC is NULL and this condition is met! So clearly what is failing is the line ‘engEvalString(ep,”[xProcess,yProcess,xC,yC] = my_matlab_function(xProcess,yProcess,xC,yC);”);’. But I don’t get it. How is it possible to fail? If I have an error in my_matlab_function this will appear on screen as an error in Matlab. Any guess?

    – S. Perez
    Nov 14 '18 at 16:39

















The question title says that the issue is about creating empty arrays. Have you done any tests that narrows it down to that problem or is it a guess? also, there is a typo memcpy(XC, mxGetData(xC),mRowsProcess*sizeof(double)); that should be memcpy(XPROCESS, mxGetData(xProcess),mRowsProcess*sizeof(double));

– Brice
Nov 14 '18 at 13:04






The question title says that the issue is about creating empty arrays. Have you done any tests that narrows it down to that problem or is it a guess? also, there is a typo memcpy(XC, mxGetData(xC),mRowsProcess*sizeof(double)); that should be memcpy(XPROCESS, mxGetData(xProcess),mRowsProcess*sizeof(double));

– Brice
Nov 14 '18 at 13:04














I have done tests in order to get which line of the code causes the segmentation fault in C. And I narrowed it down to the mxGetM(xC) line. When I don't fill xC in Matlab I don't get any errors. So my question is more if this is the proper way to fill an empty array in a C code calling a Matlab function.

– S. Perez
Nov 14 '18 at 15:07





I have done tests in order to get which line of the code causes the segmentation fault in C. And I narrowed it down to the mxGetM(xC) line. When I don't fill xC in Matlab I don't get any errors. So my question is more if this is the proper way to fill an empty array in a C code calling a Matlab function.

– S. Perez
Nov 14 '18 at 15:07













xC is created in C, then passed to the Matlab engine. What gets out of that may be totally unrelated to what gets in (code should still work with separate variables xC_in and xC_out). Actually, you could have a Matlab function that does not take any argument and still returns some variable xC. Have you examined what actually gets in the Matlab function? You could use engEvalString(ep, "save('dump_input.mat','xProcess','yProcess','xC','yC');"); to check what actually gets into the Matlab function (and another one just after to dump the output). Or do that from within the Matlab function.

– Brice
Nov 14 '18 at 15:34






xC is created in C, then passed to the Matlab engine. What gets out of that may be totally unrelated to what gets in (code should still work with separate variables xC_in and xC_out). Actually, you could have a Matlab function that does not take any argument and still returns some variable xC. Have you examined what actually gets in the Matlab function? You could use engEvalString(ep, "save('dump_input.mat','xProcess','yProcess','xC','yC');"); to check what actually gets into the Matlab function (and another one just after to dump the output). Or do that from within the Matlab function.

– Brice
Nov 14 '18 at 15:34





1




1





engGetVariable will return a NULL pointer in case of failure, which would be a cause for the seg fault with mxGetM. You should test that. The variables should exist in Matlab engine workspace regardless of your Matlab function success or failure, though, since they have the same name as the input variables. That is, as long as the calls to engPutVariable succeeded (i.e. returned 0). You should test that too. Replace engPutVariable(ep,"xC",xC) with if(engPutVariable(ep,"xC",xC)) printf('Error putting xCn');fflush(stdout);

– Brice
Nov 14 '18 at 15:46






engGetVariable will return a NULL pointer in case of failure, which would be a cause for the seg fault with mxGetM. You should test that. The variables should exist in Matlab engine workspace regardless of your Matlab function success or failure, though, since they have the same name as the input variables. That is, as long as the calls to engPutVariable succeeded (i.e. returned 0). You should test that too. Replace engPutVariable(ep,"xC",xC) with if(engPutVariable(ep,"xC",xC)) printf('Error putting xCn');fflush(stdout);

– Brice
Nov 14 '18 at 15:46














You nailed it! But I still don’t know what is failing. So I saved the input before sending it to Matlab and everything is right. However, saving the output coming from Matlab function saved nothing. Moreover I added a condition if xC is NULL and this condition is met! So clearly what is failing is the line ‘engEvalString(ep,”[xProcess,yProcess,xC,yC] = my_matlab_function(xProcess,yProcess,xC,yC);”);’. But I don’t get it. How is it possible to fail? If I have an error in my_matlab_function this will appear on screen as an error in Matlab. Any guess?

– S. Perez
Nov 14 '18 at 16:39






You nailed it! But I still don’t know what is failing. So I saved the input before sending it to Matlab and everything is right. However, saving the output coming from Matlab function saved nothing. Moreover I added a condition if xC is NULL and this condition is met! So clearly what is failing is the line ‘engEvalString(ep,”[xProcess,yProcess,xC,yC] = my_matlab_function(xProcess,yProcess,xC,yC);”);’. But I don’t get it. How is it possible to fail? If I have an error in my_matlab_function this will appear on screen as an error in Matlab. Any guess?

– S. Perez
Nov 14 '18 at 16:39













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299284%2fc-and-matlab-create-empty-array-in-c-calls-matlab-function-to-fill-it-ends-in-e%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299284%2fc-and-matlab-create-empty-array-in-c-calls-matlab-function-to-fill-it-ends-in-e%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20