if statement in embedded function

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

 Respuesta aceptada

Kaustubha Govind
Kaustubha Govind el 16 de Jul. de 2013
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 comentarios

cmcm
cmcm el 16 de Jul. de 2013
Editada: cmcm el 16 de Jul. de 2013
it did not work as i want lets say that the input
M =[ 1 2 3 4 5 6 7 8 9 ] the output that i need is like : P1=1(ramp) P2=0(constant) first step P1=1(constant) P2=2 (ramp) second step P1=3 (ramp) P2=2(constant) 3rd step P1=3(constant) P2=4(ramp) 4th step and so on
the code that u give it make both output as a ramp (as the input) with same values.. but what i want is for P1 there is a step it is ramp and the next step it is constant (with old value ) and for P2 it is opposite to P1 .. mean when P1 is at constant value the P2 is at ramp and vise verse
thanx alot
I assume @Kaustubha meant equality operator instead of assignment, i.e.
L1 == 1 && S1 == 0
and similarly on other if statement conditions.
@Shahad could you try with this change?
cmcm
cmcm el 16 de Jul. de 2013
yes i already use (==) in my program but i write it here (=) just to clarify
Muthu Annamalai
Muthu Annamalai el 16 de Jul. de 2013
That is extremely confusing!
Kaustubha Govind
Kaustubha Govind el 17 de Jul. de 2013
Oops! Yes, thanks for the correction, Muthu!

Iniciar sesión para comentar.

Más respuestas (1)

cmcm
cmcm el 17 de Jul. de 2013

0 votos

thanx Kaustubha Govind i know how to use the code that you give it :)

Categorías

Más información sobre Using MATLAB Projects in Simulink en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 16 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by