フィルターのクリア

if statement in embedded function

8 ビュー (過去 30 日間)
cmcm
cmcm 2013 年 7 月 16 日
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 件のコメント
Muthu Annamalai
Muthu Annamalai 2013 年 7 月 16 日
That is extremely confusing!
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 Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by