How can the previous value of a block input be accessed in a tlc file?

[EDIT: 20110610 20:22 CDT - reformat - WDR]
I am writing a tlc file and inside %function Update(block, system) Output, I would like to use both the current value and previous value of the block input as
d = %<LibBlockInputSignal(0, "", "", 0)> - (the value of %<LibBlockInputSignal(0, "", "", 0)> at the previous sampling instant);
I can access the current value using %<LibBlockInputSignal(0, "", "", 0)> but I do not know how to access the previous value. I cannot use
d = %<LibBlockInputSignal(0, "", "", 0)> - d;
since d is a local variable initialized to zero (or something else) inside the function and the above statement is equivalent to
d = %<LibBlockInputSignal(0, "", "", 0)>;
Is there a function I can use for this purpose? How can I make d a global variable so that it retains its previous value?

Answers (1)

There is no direct way to access the previous input of an S-function in TLC, just as there is no way to access it in the corresponding MATLAB or C/C++ source code definition of the S-function. You need to create a work vector, and store the value of the input in the current time-step for use in the next time-step.
To see a C-MEX S-function (with TLC) example of storing the previous input in a work vector, type the model name at the MATLAB prompt:
>> sfcndemo_sfunmem

Asked:

on 11 Jun 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!