Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dwork vectors not passing values properly between functions

42 views
Skip to first unread message

Kate J.

unread,
Mar 6, 2012, 3:13:14 PM3/6/12
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.

Phil Goddard

unread,
Mar 6, 2012, 8:30:24 PM3/6/12
to

mdlInitializeConditions gets called _after_ mdlStart.
So in your example I would expect the work vector to be zero.

See http://www.mathworks.com/help/toolbox/simulink/sfg/f8-37326.html for more info.

> 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?

yes there is -- and you are already using it.

Phil.

Kate J.

unread,
Mar 7, 2012, 2:21:16 PM3/7/12
to
>> mdlInitializeConditions gets called _after_ mdlStart.
>> So in your example I would expect the work vector to be zero.

Thanks for the clarification, Phil. I had been confused by the fact that the Dwork vectors must be declared in mdlInitializeSizes() before they're used in mdlStart(), so I had assumed that they were initialized in mdlInitializeConditions() before mdlStart() executed. The link you included is useful.

> 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?

>> yes there is -- and you are already using it.

Can you please clarify? All I'm currently doing, after declaring & initializing each Dwork vectory, is reading in each Dwork vector into any function it's used in, with a line similar to

real_T *Dwork_myVar = (real_T*) ssGetDWork(S,0);

and then updating each Dwork vector as necessary within that function. Was your answer above meant to imply that the system simply saves every Dwork variable at every timestep, with no explicit "save" command from me? Thanks for the clarification.

Phil Goddard

unread,
Mar 7, 2012, 3:15:17 PM3/7/12
to

>
> real_T *Dwork_myVar = (real_T*) ssGetDWork(S,0);
>
> and then updating each Dwork vector as necessary within that function. Was your answer above meant to imply that the system simply saves every Dwork variable at every timestep, with no explicit "save" command from me? Thanks for the clarification.

Sort of.
It was meant to indicate that by doing the above you are getting the pointer to the work vector and then by writing to that pointer you are saving the data.

Unlike in m-code (which doesn't have pointers) there is no need to do a guidata(h,newstuff) every time you want to update the saved data (and before using it in another function).

Phil.
0 new messages