Variable ... is undefined on some execution paths

hello guys!
I hope somebody can advise me with respect the next code, when I run it into embedded editor from simulink embedded block, I have the error that Variable 'mu1' is undefined on some execution paths.
function [u_F,mu12,mu32] = fcn(s,delta)
%#codegen
a = 0.5;
mu12 = 0;
mu32 = 0;
y = 0;
if s <= -a
mu1 = 1;
mu2 = 0;
mu3 = 0;
elseif -a < s && s <= 0
mu1 = -1/a*s;
mu2 = 1/a*(s+a);
mu3 = 0;
elseif s > 0 && a >= s
mu1 = 0;
mu2 = -1/a*(s-a);
mu3 = 1/a*s;
elseif s > a
mu1 = 0;
mu2 = 0;
mu3 = 1;
end
mu12 = mu1;
mu32 = mu3;
y = (mu1*(-delta)+mu2*0+mu3*delta)/(mu1+mu2+mu3);
u_F = y;

 採用された回答

Ryan Livingston
Ryan Livingston 2015 年 4 月 27 日
編集済み: Ryan Livingston 2015 年 4 月 27 日

5 投票

As the error says, there is a possible execution path on which mu1 is not defined. In particular, if all of the conditions in your if and elseif statements are false, then mu1,mu2,mu3 will not be defined.
To resolve this define them to default values either before the if:
mu1 = 0; mu2 = 0; mu3 = 0;
if s<= -1 ...
or add an else branch:
elseif s > a
mu1 = 0;
mu2 = 0;
mu3 = 1;
else
mu1 = 0;
mu2 = 0;
mu3 = 0;
end
The documentation explains this error message in more detail.

3 件のコメント

Smaghuli
Smaghuli 2015 年 4 月 27 日
Thank you. I already find that by adding else the problem can be solved. But this does not work on MATLAB 2008a. I I tested the model on 2013a and it worked. I will try giving initial values before the conditional statement to see it can solve the problem in older version.
Ambe Harrison
Ambe Harrison 2022 年 7 月 19 日
Thank you Ryan Livingston your comment was very useful. It just solved a similar problem i encoutered.
Kind regards.
Elisa Micaela Rodriguez Steinbrecher
Elisa Micaela Rodriguez Steinbrecher 2022 年 12 月 1 日
Amazing, thanks for your answer

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by