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 个评论

I am also trying to access base workspace / global variable from C-Mex S-function. Any help is greatly appreciated.
"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.

请先登录,再进行评论。

回答(1 个)

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.

类别

在 帮助中心 和 File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

提问:

2014-2-5

编辑:

Jan
2018-3-2

Community Treasure Hunt

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

Start Hunting!

Translated by