0

I have this function writen in matlab:

function out = summa(in1,in2)
out = in1(1)+ in1(2)+ in1(3)+ in1(4)+ in1(5)+ in1(6)+ in2(1)+ in2(2)+ in2(3)

And I've implemented it in simulink as follows:

Simulink diagram

And inside the matlab function block I've writen

summ(u(1),u(2))

I get the following error:

Simulink Error

The function works fine if I input the vectors from the console, as follows:

summa([1 2 3 4 5 6],[1 2 3])

I get 27 as output

What am I doing wrong? I have a feeling the mux does not work like I want it to or that the arguments to the block are incorrect.

1 Answers1

3

You are correct - the mux block isn't doing what you think.

The input to the Interpreted MATLAB block is a 9 element vector, with u(1) and u(2) being the first two elements of that vector. Hence in the function in1 and in2 are both scalars and you can't access more than the first/only element of them. Trying to access in1(2), etc, throws the error you are seeing.

You should use a MATLAB Function block with the following code within it,

function y = fcn(in1,in2)

coder.extrinsic('summa'); % This allows you to call the  external function
y = 0;  % This tells Simulink that the output will be a double
y = summa(in1,in2);

You'll see that the block has 2 inputs, and you should feed the output of your constant blocks into them separately.

Or even better, if possible, don't use the external function at all. Put all of your code into the function within the MATLAB Function block,

function out = fcn(in1,in2)

out = in1(1)+ in1(2)+ in1(3)+ in1(4)+ in1(5)+ in1(6)+ in2(1)+ in2(2)+ in2(3);
Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Thank you I undestand now why it fails. I've modified the summa(u(1),u(2)) to summa(u(1:6),u(7:9)) and it works like I want it to. I am using the intepreted Matlab Function because my tutor told me its better for passing code from one place to another without problems. And also the function I use is generated automaticly by matlab using the matlabFunction, I simplifyed the problem to it would be more understandable. Thank you again :) – Richard Haes Ellis Dec 11 '18 at 11:04