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

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 Comments

So if s is a vector, you do not want to do any shifting, right?
If s is a vector, I will just count the number of negative entries, and then shift the y by this amount
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;.
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.

Sign in to comment.

 Accepted Answer

Run the attached model and let me know the results.

16 Comments

Also mine is working in this way. But, if I put in "constant" a vector like [-1 -1 0 1 1 ], then Simulink does not work... I guess that the Matlab function block is not compiled in the same way as a standard matlab function.. ?
Is it not working in the way that you want, or does it give an error?
Sorry, I was not checking also on your function. Your function is working! Wait a sec, so that I can understand and thank you properly
If it helped, can you accept it?
So, your function is perfectly working! Could you be so nice and tell me why Simulink did not like my function? Was it not doing the same?
You were overwriting initial s vector, Simulink does not like it. Instead, I defined a new one and solved the issue.
Thank you very much!I am learning a lot from you!
Hi, not sure if I should open a new question or not. I would like to know why, if I look at the output of the matlab function block in matlab, I see a table, where my vector is basically horizontally replicated, for each time sample. Instead, I would have expected to get only two vectors as output: one for the time on the left, and my vector on the right. In case I should open a new question for this, just let me know. Best
You do not need to. How did you save your data to workspace, just tell me that.
I have used the "to workspace" block because the "scope" was not working. My input signal is a current, that has as many samples as the time, so I was expecting to be able to plot it.
Save output format should be Structure with time and please specify a reasonable sampling time for your signal, something like 0.001.
In the Data Import/Export, I selected now the format you said, but I do not find the output variable "yout" in the workspace. I attach the figures.
I also took away the "to workspace" and used just a "display".
Okay I do see my output now. Sorry, I am not yet used to Simulink. In yout, I see the time and, inside the structure signals, a matrix "values" that is a matrix 501 x 501
You can reach values inside struct by the notation
.signals.values(:,1)
1 stands for the column number.
Thank you. I think I understood now what I can expect when I work with constants.
You are welcome, Maria.

Sign in to comment.

More Answers (1)

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 Comments

In the comment of the above answer, I modified the function. The function is fine for me in Matlab, I just do not know why is Simulink not fine with it. So I was wondering if there are some constraints about the command I can put in a MATLAB function block (in Simulink).
KL
KL on 12 Dec 2017
Edited: KL on 12 Dec 2017
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.

Sign in to comment.

Products

Asked:

on 12 Dec 2017

Commented:

on 13 Dec 2017

Community Treasure Hunt

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

Start Hunting!