C Mex file cannot access workspace variables
Show older comments
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 Comments
Nirav Ambaliya
on 2 Mar 2018
I am also trying to access base workspace / global variable from C-Mex S-function. Any help is greatly appreciated.
Answers (1)
Jan
on 2 Mar 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.
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!