About "Matlab Fctn" block

1 回表示 (過去 30 日間)
William
William 2014 年 4 月 16 日
コメント済み: William 2014 年 4 月 16 日
I included a for loop in the ML Fctn block.
function [xtest] x=(k)
for i=1:10
x=i
if (i>5)
x=k
else
x=i
end
end
I connected the output (x) to a "Time Scope" block. After the model runs for 20 sec, the Time Scope plot only the final value of x (x=k) instead of different values of x (1,2,3,4,5,5,5,..). How can I store or plot out all values of x? disp(x) will display values of x in Matlab window but I want to either store or plot x out (not just the final value).
Thanks.
Thanks.
I connected the output (x)

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 16 日
That's because the output of your Matlab Function block is the result of your for loop. The output is not the result of each iteration. You can use persistent declaration without a for loop, because Simulink will run your function automatically each step.
function x=fcn(k)
persistent i
if isempty(i)
i=0
end
i=i+1
x=i
if i>5
x=k
else
x=i
end
  1 件のコメント
William
William 2014 年 4 月 16 日
Because I want to increase the value of x (similar to using a ramp function which is not availabe in Matlab) so I used for loop. How can I get around this?

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by