How to enable function is condition is true and ignore its updates till another condition is true
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
i need to write this code if input1-input2>5 if condition is true, the output should equal to zero even when input1-input2<5 till input1=input3 then it equal 1 and return to if condition again. in other words if input1-input2>5 is true its update will be ignored till input1=input3
0 Commenti
Risposte (1)
Bob Thompson
il 6 Mar 2018
output = 0;
if input1 == input3;
output = 1;
if input1-input2 > 5;
output = 0;
end
end
You can precondition the output however you want, but by nesting the input1-input2 condition inside of the input1=input3 condition it prevents the condition from being considered until input1=input3.
Alternatively, if you want the condition to be based on the output something like the following could work.
if output == 0;
if input1 == input3;
output = 1;
end
elseif output == 1;
if input1 - input2 > 5;
output = 0;
end
end
This will check for the output condition specifically and ignore the inputs unless the output is a certain value. Fair warning that in both of these conditions the output only updates once per loop, assuming you have a loop, and so will not check for input1=input3 and then input1-input2>5. You could copy the input1input2 if condition into the first output=0 condition (following the input1input3 condition) if you needed it to be considered in the same loop.
4 Commenti
Bob Thompson
il 6 Mar 2018
You're going to need to run some kind of variable loop inside your if statement if you want to consider multiple situations where your variables do or don't meet a condition. If you would be willing to post some of your code that you are working with I might be able to help more.
Vedere anche
Categorie
Scopri di più su Multibody Modeling in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!