MATLAB Answers

0

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

Prasath Ayyalraj さんによって質問されました 2018 年 11 月 13 日
最新アクティビティ Walter Roberson
さんによって 再度開かれました 2018 年 12 月 20 日
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 件のコメント

サインイン to comment.

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 件のコメント

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

サインイン to comment.



Translated by