How to retain the last known values of Matlab function block’s Outports without using the trigger subsystem

9 views (last 30 days)
Sample time of the model is 0.001sec but the Matlab function that I am using is with 2sec. I wanted to run particular code inside the Matlab function whenever the trigger input value becomes 1. When the trigger input value becomes 0, I want the outports of this Matlab function to retain the last known value. I am not able to use the trigger type subsystem as the Matlab function inside it runs at different sample time.
Now either I have to use both triggered and different sample time here or i should get a way to retain the last known value. Can anyone help me on this.
below is the format that i am using now.
function [out] = fcn(in)
if(in == 1)
out = 1;
else
% does nothing but during this part of execution, I want the variable out to retain its last known value (also as the variable out is not used in this path of execution but only in if statement, matlab throws an error.
end
end

Answers (1)

Arunkumar M
Arunkumar M on 13 Nov 2018
Pass the output of the function also as an input.
x = fcn(in,x)
Thereby you can retain the output of function with previous value as shown below:
function [out] = fcn(in,out_prev)
if(in == 1)
out = 1;
else
out = out_prev;
end
  1 Comment
Arunkumar M
Arunkumar M on 13 Nov 2018
Use unit delay block as shown below, this will solve algebraic loop issue.
function y = fcn(u, trigger, y_prev)
if(trigger == 1)
y = u;
else
y = y_prev;
end
Capture.JPG

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!