MATLAB Answers

Variable 'KD' is not fully defined on some execution paths

5 views (last 30 days)
willy coronel
willy coronel on 19 Oct 2020
Edited: Stephen Cobeldick on 20 Oct 2020
function KD = fcn(delta_KD)
persistent KD_actual;
persistent KD_anterior;
if isempty(KD_actual)
K=2;
t0=8;
tao=4;
KD=(0.51/abs(K))*(tao/t0)^0.76;
KD_actual=0
KD_anterior=0;
end
KD_anterior=KD;
KD_actual=delta_KD;
KD=KD_anterior+KD_actual;

  0 Comments

Sign in to comment.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 20 Oct 2020
Edited: Stephen Cobeldick on 20 Oct 2020
Look at this line of code
KD_anterior=KD;
and now consider what is the value of KD the second time the function is called. The first time the function is called the variable KD_actual is empty and so the code inside the if block is evaluated and so KD is defined. But the second time you call the function KD_actual will not be empty (you just defined it with the first call and persistent keeps whatever value it had) and so the if block does not run and so KD is not defined (which is why MATLAB warns you about this).

  0 Comments

Sign in to comment.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!