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

조회 수: 18 (최근 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

답변 (1개)

Arunkumar M
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
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by