C Mex file cannot access workspace variables

I have 2 arrays declared on the base workspace (x_values and y_values), both are 499,1. I am simply trying to use the mdlOutputs function to access them and print them on the workspace but its not working. Code is below;
static void mdlOutputs(SimStruct *S, int_T tid)
{
mxArray *x_ptr;
mxArray *y_ptr;
real32_T* pOut;
real32_T xInput;
real32_T* xVals;
real32_T* yVals;
real32_T index;
/* Get a pointer to the data stores */
x_ptr = mexGetVariablePtr("base", "x_values");
y_ptr = mexGetVariablePtr("base", "y_values");
if ( (x_ptr == NULL) || (y_ptr == NULL))
{
mexErrMsgIdAndTxt( "MATLAB:mxislogical:getVariableFailed",
"Could not get the variable named .....\n");
}
/* Point to the first element of real data */
xVals = mxGetPr(x_ptr);
yVals = mxGetPr (y_ptr);
for(index = 0; index < 400; index++)
{
mexPrintf("Y values array %f \n", *yVals);
yVals++;
}
}
It seems like mexGetVariablePointer is not returning the correct address.... but its also not returning NULL..

2 comentarios

Nirav Ambaliya
Nirav Ambaliya el 2 de Mzo. de 2018
I am also trying to access base workspace / global variable from C-Mex S-function. Any help is greatly appreciated.
Jan
Jan el 2 de Mzo. de 2018
Editada: Jan el 2 de Mzo. de 2018
"It is not working" is not a useful explanation. "It seems like mexGetVariablePointer is not returning the correct address" does not clarify also, what you observe. Please post, what happens.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 2 de Mzo. de 2018
real32_T* xVals;
xVals = mxGetPr(x_ptr);
mxGetPr returns a pointer to a double array, but you store it in a reat32_T pointer. Use this instead:
double* xVals;
xVals = mxGetPr(x_ptr);
and the same for yVals.

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 5 de Feb. de 2014

Editada:

Jan
el 2 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by