Using matlab function in simulink error
1 次查看(过去 30 天)
显示 更早的评论
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
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
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 个评论
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!