Clear Filters
Clear Filters

Using matlab function in simulink error

3 views (last 30 days)
Leonardo Vettore
Leonardo Vettore on 8 Aug 2016
Commented: Tiny Feng on 5 Jul 2018
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.
  1 Comment
Tiny Feng
Tiny Feng on 5 Jul 2018
Thank for your answer, I think your code -persistent y- is the key for this problem. I also accounted similar problem, and by your code I solved my problem successfully.

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 8 Aug 2016
Your Matlab Function block is called each step time, and it depends on the value of the variable input_signal which is a scalar, then you can't use lsim function with one value, the error message says that you need at least two values! In your case you can use lsim at the end of your simulation. Can you provide more information on what you want to do.
  2 Comments
Leonardo Vettore
Leonardo Vettore on 8 Aug 2016
I would like to call this function each step time with all the signal from the previous and the current step times. So for example if I have 10 steps and a total input signal that is [1 2 3 4 5 6 7 8 9 10] I would like to launch the function the first time with an input signal [1], the second with [1 2] the third with [1 2 3] and so on. Since lsim needs at least two values for the first time I can use a default value as output
Azzi Abdelmalek
Azzi Abdelmalek on 8 Aug 2016
function outputSignal = myFunction(input_signal)
coder.extrinsic('lsim');
persistent y
B = [1 0]';
C = [1 1];
D = 0;
k_dia = [19.5 13 9.9];
k_dia = k_dia*10^-3;
y=[y;input_signal];
if numel(y)>=2
time = [1:1:length(input_signal)];
outputSignal = zeros(length(time), length(k_dia));
for j = 1:length(k_dia)
A = [-k_dia(j) 0; k_dia(j) -k_dia(j)];
sistem = feval('ss', A, B, C, D);
outputSignal(:,j) = lsim(sistem, input_signal, time);
end
else
outputSignal=0;
end

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!