MATLAB: Is there an example that explains how to use a PWork vector in an S-function in Simulink 7.3 (R2009a)

pworksimulink

I need to use a PWork vector in my S-Function. However, I am not able to find any useful information or a simple example in the documentation regarding the same.

Best Answer

The ability to use a PWork vector in an S-function is available in Simulink 7.3 (R2009a). Download the attached files for a complete example that illustrates the setup and very simple use of a PWork vector in an S-Function. It uses a PWork vector to store a counter.
Refer to the steps below, which is a snippet of the attached code, in order to use a PWork vector in an S-function.
1) Set the number of pointers in the PWork vector.
ssSetNumPWork
2) Allocate the memory.
p_work_vector_target_data_handle = (double *) malloc( sizeof(double) );
3) Initialize the counter to 0.
(*p_work_vector_target_data_handle) = 0.0;
4) Retrieve the PWork vector value.
p_work_vector_element = ssGetPWorkValue(S, 0);
5) Increment and set the PWork vector value.
(*p_work_vector_element) = (*p_work_vector_element) + 1
(*output_port_data_pointer) = (*p_work_vector_element);
6) Set the PWork vector to NULL.
void ** p_work_vector = NULL;
7) Clear the memory.
free(p_work_vector_element);