how to read in variable from workspace in s-function written in C without simulink crashing
Show older comments
When I add the following code to my mdlOutputs function, the mex compiler does not give any errors, but when I run my simulink model it crashes... any idea what I am doing wrong? I am trying to read in the variable temperature from my base workspace and then accessing the third element (in reality I would like to have the element that I am accessing dynamically changing but I will worry about this problem later when the static code finally works)
The variable temperature is a 730x2 matrix, when I try to define temperature[1] [2] as I would expected I needed since there are two dimension, the compiler does not work so I think there is a problem with indexing. The help of the mxGetPr does not really help me: "Call mxGetPr to access the real data in the mxArray that pm points to. Once you have the starting address, you can access any other element in the mxArray." The latter might be obvious to others, but I do not see how I can do this so any example code is highly appreciated...
real_T *temperature, *currentTemperature;
char array_name1[20] = "temperature";
const mxArray *array_ptr1;
array_ptr1 = mexGetVariablePtr("base", array_name1);
if (array_ptr1 == NULL){
mexErrMsgIdAndTxt( "MATLAB:mxislogical:getVariableFailed",
"Could not get the variable named temperature.\n");}
temperature = mxGetPr(array_ptr1);
*currentTemperature = temperature[3];
mxFree(array_name1)
y[0] = *currentTemperature;
Accepted Answer
More Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!