I have a Matlab function block in Simulink that receives 2 inputs and processes it to generate an output. During the course of the simulation, at some time points, one of the inputs is zero. I'd like to use the most recent non-zero input to the function whenever that particular input value is zero. How can I achieve this? I tried creating a persisent variable that updates to most recent non-zero input value but that doesn't seem to work.
EDIT 1 (to include code):
function y = fcn(u)
persistent ref_val
if isEmpty(ref_val)
ref_val=10.0
end
if(u(1)<=25)
y=20.0
else
if(u(2)>0)
y=u(2)
ref_val=u(2)
else
y=ref_val
end
end
EDIT 2: For now, I fixed the issue by writing a C code that uses a static variable to retain the most recent non-zero input value. But I still welcome suggestions/solutions to realize this directly in a Matlab function.