why does an "if" statement inside a MATlab function block in simulink not trigger?
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 2 de Ag. de 2018
Editada: Walter Roberson
el 2 de Ag. de 2018
I would recommend double-checking that k is a scalar.
assert(length(k) == 1)
and non-nan
assert(~isnan(k))
Johan Prent
el 2 de Ag. de 2018
Dennis
el 2 de Ag. de 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
el 2 de Ag. de 2018
Adam
el 2 de Ag. de 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
el 3 de Ag. de 2018
Respuestas (0)
Categorías
Más información sobre Simulink en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!