フィルターのクリア

Infinite loop in Matlab Fctn Block

1 回表示 (過去 30 日間)
William
William 2014 年 4 月 22 日
コメント済み: Niklas Nylén 2014 年 4 月 22 日
Why did I run into infinite loop (see coode below)? Also, I tried to stop the simulation by clicking on the stop button on the model window but it didn' work. i is the input from a ramp function in Simulink model. Thanks
function [y1,y2] = UPC(i) %#codegen persistent ii ii=0; if isempty(ii) y1=ii; y2=ii; else y1=i; y2=i; end while (i<5) y1=i+1; y2=i-1; end_ *_ *

採用された回答

Niklas Nylén
Niklas Nylén 2014 年 4 月 22 日
編集済み: Niklas Nylén 2014 年 4 月 22 日
The condition to exit the while loop is that i>=5 and you never change the value of i in the loop, which means you will never exit the loop if the input value of i is less than 5.
For every time step in your model the code in an embedded matlab code block will run from the first line of code to the last. What it looks like is that you want to perform the operation inside the while loop if i<5, not while i<5.
  2 件のコメント
William
William 2014 年 4 月 22 日
Why is i not changing? i is the input from a ramp function (slope 1). Shouldn't i be increase as simulation time increase?
Niklas Nylén
Niklas Nylén 2014 年 4 月 22 日
No, since all the code in the embedded code block will run on every iteration of the model. What I'm guessing you would like to do is something like this:
function [y1,y2] = UPC(i)
%#codegen
if i<5
y1=i+1;
y2=i-1;
else
y1=i;
y2=i;
end
The code above will run once for every time step of the simulink model. You do not need to add a loop inside the code to keep it running with the model.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEvent Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by