why does an "if" statement inside a MATlab function block in simulink not trigger?
Show older comments
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 Comments
Walter Roberson
on 2 Aug 2018
Edited: Walter Roberson
on 2 Aug 2018
I would recommend double-checking that k is a scalar.
assert(length(k) == 1)
and non-nan
assert(~isnan(k))
Johan Prent
on 2 Aug 2018
Dennis
on 2 Aug 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
on 2 Aug 2018
Adam
on 2 Aug 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
on 3 Aug 2018
Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!