if statement in embedded function

8 次查看(过去 30 天)
hello every one i have a program with 5 inputs named (L1,L2,S1,S2,M) and 2 outputs named (P1,P2) M is a ramp input and L1= 1 or 0 (pulse input) L2= 1 or 0 (pulse input) S1= 1 or 0 (pulse input) S2= 1 or 0 (pulse input) i want to writ a program in embedded function that will pass the first value of M at P1 when L1=1 and S1=0 (and what ever was the other inputs) in the firs step and in next step the value of M will pass to P2 when L2=1 and S2=0 and at the same time P1 will remain with its old value from the first step (what ever the value of other inputs was) in 3rd step the program will be repeated so M value will be pass to P1 ( and P2 will remain with its old value from the 2nd step) and thet also happened when L1=1 & S1=0 step 4 will be as step 2 and so on i did not use for loop because my time is not constrain
the program is like written bellow
function [P1,P2]=fcn(L1,L2,S1,S2,M)
if L1=1 && S1=0
P1=M
else
P1=0 %%here it should not equal to 0 it should be a past value ... how can i make it have an old value ?
if L2=1 && S2=0
P2=M
else
P2=0
end
as i write in the previous program the output of P1 will be [ value1,0,value3,0,value5,0,....] and the output of P2 will be [0,value2,0,value4,0,value6,0,....] but i want it to be : P1=[value1,v1alue1,value3,value3,value5,value5,......] P2=[0,value2,value2,value4,value4,value6,value6,.....]
please anyone can help ? thanx alot

采纳的回答

Kaustubha Govind
Kaustubha Govind 2013-7-16
You can use a persistent variable to store the previous value of the input. For example:
function [P1,P2]=fcn(L1,L2,S1,S2,M)
persistent X;
if isempty(X) % only run the first time the function executes
X = 0; % since there is no previous 'M' available the first time, set to 0
end
if L1=1 && S1=0
P1=M;
else
P1=X;
end
if L2=1 && S2=0
P2=M;
else
P2=X;
end
X=M; %store current input for next iteration
  5 个评论
Kaustubha Govind
Kaustubha Govind 2013-7-17
Oops! Yes, thanks for the correction, Muthu!

请先登录,再进行评论。

更多回答(1 个)

cmcm
cmcm 2013-7-17
thanx Kaustubha Govind i know how to use the code that you give it :)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by