Detect Inf or Nan in Embedded Matlab Function block

Is there a way to detect an Inf or Nan being calculated inside an Embedded Matlab block. Simulink does not produce an error, but when we generated code, an exception was found. We are looking to protect against them. The problem can be reduced to the following.
[Constant block=0] --> [Embedded MATLAB Function] --> [Display]
Embedded MATLAB Function
function y = fcn(u)
test = 1/u;
if test > 0
y = 1;
else
y = 0;
end
"Configuration Parameters > Diagnostics > Data Validity > Inf or NaN block output", is just that, checking block output, and does not report this. (Which surprised me.)
Note: Simulink 6.6.1
Thanks.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 7 日

0 投票

Use isnan or isinf function

4 件のコメント

Albert Yam
Albert Yam 2013 年 2 月 7 日
編集済み: Albert Yam 2013 年 2 月 7 日
Thanks, that does work. Any Idea, about the code portion of it though? Since this requires the inf/nan to be calculated, Simulink will throw the error, but in the code form, once the inf/nan is calculated problem occurs.
Hm.. max(u,eps) ..?
Edit: Going with
u_sign = sign(u);
if ~u_sign
u_sign = 1;
end
u = u_sign*max(abs(u),eps);
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 7 日
What do you mean? I did not get it
Albert Yam
Albert Yam 2013 年 2 月 8 日
isnan and isinf require that the variable carry a value of inf or nan. On our generated code being run on our target platform, when that occurs there is already an exception. It never gets a chance to check for inf or nan.
So my protection, is an attempt to prevent the inf from occurring.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 8 日
Maybe there is a way to disable such error message in configuration parameters. I will check it.

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

その他の回答 (0 件)

カテゴリ

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

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by