Persistent variable 'count' is undefined on some execution paths when using embedded matlab function

11 ビュー (過去 30 日間)
I am using the embedded matlab function with constant 1 as the first input and a step signal as the second input (initial value to be 1 and then after a stepsize it becomes 0). The code for the matlab function is as follows:
function sys = fcn(u)
persistent count;
if u(2)>0
count=0;
else
count=count+0.01;
end
sys=u(1)+count;
end
The error shows that: Persistent variable 'count' is undefined on some execution paths**
When I changed the embedded matlab function to s function and used the same code again, there is no error. Why is this code not valid within the embedded matlab function? Thanks for the reply.

採用された回答

Image Analyst
Image Analyst 2013 年 1 月 4 日
編集済み: Image Analyst 2013 年 1 月 4 日
If you enter the function with the second element of "u" less than zero, it's possible to execute the "count=count+.01" line, which may have count undefined is you never did the first part of the if block. I would think it would be a warning, not an error during editing time, but an actual error if you hit that line with count undefined during run-time.
And I don't know what an " s function" is, and I don't have the embedded toolbox so I don't know about your latter sentences.
Try it like this:
persistent count;
if isempty(count)
count = 0;
end
if u(2)>0
count=0;
else
count=count+0.01;
end
sys=u(1)+count;
  1 件のコメント
Qingbin
Qingbin 2013 年 1 月 6 日
Thanks for the reply, I used the isempty function to initialize the count and it works.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEmbedded Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by