Matlab function in Simulink - retain previous non-zero input value -
i have matlab function block in simulink receives 2 inputs , processes generate output. during course of simulation, @ time points, 1 of inputs zero. i'd use recent non-zero input function whenever particular input value zero. how can achieve this? tried creating persisent variable updates recent non-zero input value doesn't seem 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: now, fixed issue writing c code uses static variable retain recent non-zero input value. still welcome suggestions/solutions realize directly in matlab function.
can't use in simulation?
//find index of last non-zero value in input
[~,last_non_zero] = max(find(input(1:i) > 0))
//call function using input
output = fnc(input(last_non_zero))
Comments
Post a Comment