【Simulink】报错:Size mismatch (size [2 x 1] ~= size [1 x 1]). The size to the left is the size of the l

版本:Matlab2019b

背景信息:

我在 Simulink 里面的 stateflow 里面,写了一个 Matlab Function,参照了实验室往届师兄的代码,同样都是单输入单输出函数,但是用的变量大小不一样。

报错信息:

直接套用函数果然出现了问题。

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.
Component:Stateflow | Category:Coder error
Size mismatch (size [2 x 1] ~= size [1 x 1]).
The size to the left is the size of the left-hand side of the assignment.

Function 'thd_cal' (#135.1133.1141), line 47, column 1:
"thd_temp"
Launch diagnostic report.
Component:Stateflow | Category:Coder error
Error while generating code for chart Chart.
Component:Stateflow | Category:Coder error
Error while generating code for chart Chart.
Component:Simulink | Category:Model error
Error occurred in 'MPC_SPI6/Chart'.
Component:Simulink | Category:Model error

看了网上的解决方法,比如修改变量名,还是没能解决。

解决方法:

Size mismatch (size [2 x 1] ~= size [1 x 1]).
The size to the left is the size of the left-hand side of the assignment.

Function 'thd_cal' (#135.1133.1141), line 47, column 1:
"thd_temp"

从报错信息可以看出,变量 thd_temp 有问题,原本是 size [2 x 1] ,但运行结果是 size [1 x 1] .

根据报错信息提示,修改变量大小。

点击 MODELING -> Model Explorer
在这里插入图片描述

可以看到各种模块变量的信息,在 Stateflow “Chart” 下面除了 State,还有自己写的 Matlab Function “thd_cal”

在这里插入图片描述

点击"thd_cal",修改里面变量大小

在这里插入图片描述

根据报错信息提示,把 [2, 1] 改成 1 即可。

在这里插入图片描述

这时候再运行,就没有 Size 报错了。