How to retain the last known values of Matlab function block’s Outports without using the trigger subsystem
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Arunkumar M
2018-11-13
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 个评论
Arunkumar M
2018-11-13
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194851/Capture.jpeg)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!