Adding an if condition in a Matlab Function in Simulink results in error. (Suddendly it's a "size mismatch"?!)
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
So my actual code is different than the one below, but the part which results in the error is similar enough and the error I get is the same.
So the code is in the MATLAB Function in Simulink. Input is a 4x4-Matrix [1,0;0,1].
If I run the code without the "if m>1"-condition, it works perfectly. Since m is always greater than 1 in this party of the code, the condition should not change anything. But I always get the error:
"Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs."
"Size mismatch (size [2 x 2] ~= size [1 x 1]). The size to the left is the size of the left-hand side of the assignment. Function 'MATLAB Function' (#8241.125.126), line 15, column 9: "u""
I absolutly don't understand why and what I can change to make it run. Can anyone help me?
function y = fcn(u)
y = Split(u);
end
function u = Split(u)
m = size(u,1);
if m == 1
u = u;
else
if m>1
u = u(1:m/2);
end
end
end
Respuestas (1)
Kanishk
el 4 de Sept. de 2024
Hi ivosp,
I understand you are using a MATLAB function block in Simulink and are encountering a "Size mismatch" error. This error occurs because the input and output variables have the same name. You can identify the exact problem by combining the two functions as shown below:
function u = fcn(u)
m = size(u,1)
if m == 1
u = u;
else
if m>1
u = u(1:m/2);
end
end
end
This results in the following error.
"Input 'u' of entry-point function 'fcn' has size [2 x 2] but is assigned with size [1 x 1]. If you use the same variable as both input and output, its type and size must be consistent throughout the function body for code generation. The size to the left is the size of the left-hand side of the assignment."
This is because when using MATLAB function block, Simulink generates binary code or C/C++ MATLAB executable (MEX) code from the block and integrates this code with the model.
Please look at the official MATLAB documentation of “Code and Integration Limitations for MATLAB Function Blocks” to learn more about the issue.
Changing the output variable name resolves the issue.
function y = fcn(u)
y = Split(u);
end
function y = Split(u)
m = size(u,1)
if m == 1
y = u;
elseif m>1
y = u(1:m/2);
end
end
Hope this helps!!
Thanks
0 comentarios
Ver también
Categorías
Más información sobre Event Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!