MATLAB: Is there an example for using Work vector in a MATLAB file S-Function in the documentation in Simulink 6.2 (R14SP2)

dworkexamplesimulinkvectorwork

My S-function needs persistent memory storage. I am looking for an example for data storage capability for MATLAB code.

Best Answer

The following is an example code for the persistent memory storage:
The DWork vector with MATLAB files (which is equivalent to Work Vector) can be used for memory storage in Simulink. A model with 3 inputs and 2 outputs has been created from the template for Level-2 MATLAB file S-Function.
The following files are attached :
mysfcn.m
test2.mdl
For syntax in using DWork vector, refer to the function under 'DoPostPropSetup' in myfcn.m file.
The name, dimensions, datatypeID, complexity properties are defined there and the 'UsedAsDiscState' property has been set to 'false' (i.e used as continuous state).
block.Dwork(1).Name = 'x1';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = false;
Under 'Outputs' function, 'block.Dwork(1).Data' has been added, so that the algorithm is adding the value each time and increasing the value of the output. You can see that in the Scope. The parameter is set to 5 in Level-2 S-Function, you can see that in the Display output.