Using matlab function in simulink error

조회 수: 1 (최근 30일)
Leonardo Vettore
Leonardo Vettore 2016년 8월 8일
댓글: Tiny Feng 2018년 7월 5일
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
Tiny Feng
Tiny Feng 2018년 7월 5일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 8일
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
Leonardo Vettore
Leonardo Vettore 2016년 8월 8일
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 2016년 8월 8일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Signal Attributes and Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by