How to compile a Simulink file?

1 回表示 (過去 30 日間)
Stefan
Stefan 2015 年 6 月 28 日
回答済み: Walter Roberson 2015 年 6 月 29 日
Dear reader,
I am trying to get an insight into the best practice towards writing matlab code that can be used with Simulink. In other words, I would like to be able to compile in Simulink, this toy example scheme:
where inside the Matlab embedded function I call:
y = select1([1 0 2 0 3]);
The function select1.m identifies the zeros inside a vector and returns their position:
function x = select1(y)
x = [];
for i=find(y==0)
x = [x i];
end
Now, once I run the Simulink scheme it gives errors, complaining about select1.m There are 2 main problems here:
  1. the usage of variable size vectors (namely x)
  2. the for-loop is written in a way that coder cannot interpret it
In particular, my question is the following: what is the best practice towards (re)writing this simple piece of code select1.m such that the Simulink scheme described above can compile, without generating errors?
Thank you

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 6 月 29 日
Try something like
ind = find(y==0);
x = zeros(size(ind));
for k = 1 : length(ind)
x(k) = ind(k);
end
This could probably be shortened to
x = find(y==0);
but you did say it was a toy example so I will presume that the purpose is to produce a variable-length output iteratively.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by