Adding an if condition in a Matlab Function in Simulink results in error. (Suddendly it's a "size mismatch"?!)
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Kanishk
2024년 9월 4일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Event Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!