why does an "if" statement inside a MATlab function block in simulink not trigger?
Afficher commentaires plus anciens
I am trying to create a "switch" of sorts within my simulink model. I have a system that takes in 3 numbers, and if all 3 numbers are below a value, it will increase my counter (k) by 1. Once my counter reaches a value (lets say 5 for example), the block will change the value it is outputting.
The blocks in simulink are shown in the picture.
The code inside the MATLAB function is as follows:
function [R,k] = fcn(eR,eV,eA,k,rHold, rDock)
if k > 5
R = rDock;
else
R = rHold;
end
if (k <= 5)
if((abs(eR) < 0.2) && (abs(eV) < 0.1) && (abs(eA) < 0.2))
k = k + 1;
end
end
end
6 commentaires
Walter Roberson
le 2 Août 2018
Modifié(e) : Walter Roberson
le 2 Août 2018
I would recommend double-checking that k is a scalar.
assert(length(k) == 1)
and non-nan
assert(~isnan(k))
Johan Prent
le 2 Août 2018
Dennis
le 2 Août 2018
Can you provide a minimal not-working example? Are eR, eV and eA all small enough? Which if statement does not trigger?
I ran your funtion with some dummy inputs and it looked fine.
eR=0.01;
eV=0.01;
eA=0.01;
rDock=1;
rHold=2;
k=0;
for i=1:10
[R,k]=fcn(eR,eV,eA,k,rHold,rDock)
end
function [R,k] = fcn(eR,eV,eA,k,rHold, rDock)
if k > 5
R = rDock;
else
R = rHold;
end
if (k <= 5)
if((abs(eR) < 0.2) && (abs(eV) < 0.1) && (abs(eA) < 0.2))
k = k + 1;
end
end
end
Johan Prent
le 2 Août 2018
Adam
le 2 Août 2018
What happens if you add
assert( ((abs(eR) < 0.2) && (abs(eV) < 0.1) && (abs(eA) < 0.2)) )
at the top of the function?
Johan Prent
le 3 Août 2018
Réponses (0)
Catégories
En savoir plus sur Simulink dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!