0
votes

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

1 Answers

3
votes

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);