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 Center および File ExchangeSignal Attributes and Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by