Using matlab function in simulink error

312 Views Asked by At

I'm new to Simulink but I need to use a matlab function.

I created a "MATLAB Function1" block with one input (a time signal that comes from another block) and one output (three signals wrapped in a matrix shown in a Scope block).

Here the code inside the matlab function block:

function outputSignal  = myFunction(input_signal)
coder.extrinsic('lsim');

time = [1:1:length(input_signal)];

k_dia = [19.5 13 9.9];
k_dia = k_dia*10^-3;

outputSignal = zeros(length(time), length(k_dia));

for j = 1:length(k_dia)
    A = [-k_dia(j) 0; k_dia(j) -k_dia(j)];
    B = [1 0]';
    C = [1 1];
    D = 0;
    sistem = feval('ss', A, B, C, D);
    outputSignal(:,j) = lsim(sistem, input_signal, time);
end
end

Previously I had problems using the functions "ss" and "lsim" because of code generation problems, but I should have solved them using feval and coder.extrinsic. Now I have the following error:

When simulating the response to a specific input signal, the input data U must be a matrix
of numeric values with at least two rows (samples) and without any NaN or Inf.  

and I can't understand if the problem is still with these functions or if I made a mistake in how to use matlab functions in simulink.

EDIT: I understood that the problem was because lsim needs an input_signal of at least length 2, while my system is giving an input to the function of one single value at every time step. So if the time is of 10 steps and the serie of values generated from the previous block is [1 2 3 4 5 6 7 8 9 10], I would like to have as input to my function:

  • [1] at step 1,
  • [1 2] at step 2,
  • [1 2 3] at step 3, ....
  • [1 2 3 4 5 6 7 8 9 10] at step 10.

Of course since lsim doesn't work with one value, at step 1 I could use a default output value.

I think I need a block that memorizes what happens in the time steps before, something like the Scope block but with an output.

1

There are 1 best solutions below

2
On BEST ANSWER

Since your systems are time invariant you can create three separate State space systems each with different parameter valued matrices and supply a vector of three components as input signals which I have left empty because I don't know where you would like to send from.

enter image description here