Kate J.
unread,Mar 6, 2012, 3:13:14 PM3/6/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
I’m developing some c-mex s-function code, and I’m trying to stay away from using global variables. I’ve adopted the use of Dwork vectors instead, which is supposed to allow me to maintain variables that have persistent value and that can be accessed by any function.
However, I’m finding that, after I declare my Dwork vector in mdlIntializeSizes(), assign it a name in mdlInitializeConditions(), and assign it a value in mdlStart(), when I access this vector within my mdlOutputs() function, it value is always 0.0, unless I specifically assign it a nonzero value *within mdlOutputs()*. Below is a simplification of my code:
static void mdlInitializeSizes(SimStruct *S){
ssSetNumDWork(S, 1);
ssSetDWorkWidth(S, 0, 1);
ssSetDWorkDataType(S, 0, SS_DOUBLE);
}
static void mdlInitializeConditions(SimStruct *S){
real_T *Dwork_Target = (real_T*) ssGetDWork(S,0);
Dwork_Target[0] = 0.0; /* assign an initial value of 0 */
}
static void mdlStart(SimStruct *S){
real_T *Dwork_Target = (real_T*) ssGetDWork(S,0);
Dwork_Target[0] = 15.2; /* assign new nonzero value */
/* I’ve verified that this value *is* being assigned correctly here */
}
static void mdlOutputs(SimStruct *S, int_T tid){
real_T *Dwork_Target = (real_T*) ssGetDWork(S,0);
mexPrintf(“Dwork_Target[0] = %f.\n”, Dwork_Target[0]); /* always prints value = 0.0! */
}
Am I supposed to be passing the Dwork vector as an argument to each function? Or, is there some other aspect of the Dwork syntax that I’m not aware of, that is preventing me from passing values assigned in one function, to be used in another function? In my mdlOutputs() function, if I don’t specifically assign a new value to Dwork_Target[0], the value printed is always 0.0, which defeats the purpose of using the Dwork vector, if the values assigned don’t persist among function calls. Is there a “save” statement for the Dwork vector, analogous to the “guidata(hObject, handles);” statement used to save changes to the handles structure in GUIDE code? This seems like it would be useful, but I haven’t seen any such thing mentioned in the Dwork documentation I’ve read.
Thanks for any input about what’s wrong with my syntax above.