Using a From Workspace block inside an enabled subsystem

8 views (last 30 days)
When I use a "From Workspace" block inside an enabled subsystem, the source signal is referenced from the global time. Is it possible to have the From Workspace block proceed only if the subsystem is enabled and not based on the global time?
ie, Assuming Time and Signal are the same values, if I enabled the subsystem after 5 seconds, it starts with 0 and not 5. Then if I disable it at 10 seconds with the value being 5 and then re-enable at 15, the output then resumes from 5.

Answers (1)

Sebastian Castro
Sebastian Castro on 25 Dec 2015
I don't think you're able to do this directly with the From Workspace block. All these blocks output data by looking at the current simulation time.
What I would do in this situation is use a MATLAB Function block with a persistent variable that keeps track of the time it should be reading. This way, you're not using global time, and you're using the times the block executes instead.
For example, take the simple case that every time the block runs you just read the next element of an array:
function out = enabledRead
persistent idx
% Initialize the index to 1 the first time the block executes
if isempty(idx)
idx = 1;
end
% Produce output and increment counter
out = myDataVector(idx);
idx = idx + 1;
end
For something more complicated, you could instead increment a time variable instead of an index to a vector. Then, you can use functions like interp1 to look up and interpolate the data vector based on the time vector.
Easiest way to do this would be to make the block discrete, if possible. Your counter's increment would be deterministic and you wouldn't have any surprises from the block running more/less often depending on the simulation step-size.
- Sebastian

Products

Community Treasure Hunt

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

Start Hunting!