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 个评论
Nirav Ambaliya
2018-3-2
I am also trying to access base workspace / global variable from C-Mex S-function. Any help is greatly appreciated.
回答(1 个)
Jan
2018-3-2
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) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!