What can I write in a MATLAB function block in Simulink?

1 回表示 (過去 30 日間)
Maria
Maria 2017 年 12 月 12 日
コメント済み: Birdman 2017 年 12 月 13 日
Hi I am working with Simulink. I wrote a function in Matlab that basically does a circular shift, and I wanted to put it into Simulink by using the MATLAB function block. The thing is that if I write
function y= shift_with0(u,s)
y = circshift(u,s);
y(1:s)=0;
end
no problem and everything is working. But, I wanted to allow as inputs also vectors, so I wrote this function
function y= shift_with0(u,s)
if isvector(s)
pm=sign(s);
inegatif = sum(pm(:)==-1);
s = inegatif;
end
y = circshift(u,s);
y(1:s)=0;
end
In Matlab is working, but the Simulink block not. So, I assume that is the "if" part that Simulink does not accept. In general, could someone clarify for me what I can write/not write in a MATLAB function block? Thanks
  4 件のコメント
KL
KL 2017 年 12 月 12 日
編集済み: KL 2017 年 12 月 12 日
isvector should return 1, even if it had only one element. It actually helps you differentiate between a vector and a matrix. try
isvector([1 2 3])
and then
isvector(1)
Also, I do not get what you mean by y(1:s)=0;.
Maria
Maria 2017 年 12 月 12 日
編集済み: Maria 2017 年 12 月 12 日
You are right about the isvector. I modified it
function y= shift_with0(u,s)
[n,m] = size(s);
if n>1 || m>1
pm=sign(s);
inegatif = sum(pm(:)==-1);
s = inegatif;
end
y = circshift(u,s);
y(1:s)=0;
end
With y(1:s)=0 I set the components before the "s" point to zero. It is because want to shift indeed, not really to circularly shift. I found this solution that suits me. In front of the values that have been shifted, I want zero. In Matlab I get what I actually want. But Simulink complains. I attach the file.

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

採用された回答

Birdman
Birdman 2017 年 12 月 12 日
Run the attached model and let me know the results.
  16 件のコメント
Maria
Maria 2017 年 12 月 13 日
Thank you. I think I understood now what I can expect when I work with constants.
Birdman
Birdman 2017 年 12 月 13 日
You are welcome, Maria.

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

その他の回答 (1 件)

KL
KL 2017 年 12 月 12 日
If s is a vector, I will just count the number of negative entries, and then shift the y by this amount
Shouldn't you be simply writing,
...
if numel(s)>1
s = numel(s(s<0));
end
y = circshift(u,s);
...
  3 件のコメント
KL
KL 2017 年 12 月 12 日
編集済み: KL 2017 年 12 月 12 日
The difference between matlab and simulink is not only with the block based modelling but also its code generating abilities. The matlab-function block gives you the ability to do that and of course it comes with a restriction. This is why you use extrinsic command for some matlab functions inside simulink.
Unlike matlab, when you assign/reassign you have to mind certain things in simulink environment.
You can read more to understand why this happens. Documentation is the best place to learn how things work.
Maria
Maria 2017 年 12 月 12 日
Thanks!

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by