Using matlab function in simulink error
1 view (last 30 days)
Show older comments
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
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.
Answers (1)
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
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!