MATLAB: How to resolve an error about variable sized data in the MATLAB Function block

simulink

I am trying to implement a MATLAB Function block with variable-sized output, but I get the following error when I try to update my model:
Output 'y' has variable size but the upper bound is not specified;
explicit upper bound must be provided.

Best Answer

If the size of the variable can change during run-time:
You need to set up the sizes in the Data Editor for the MATLAB Function Block as per:
In the editor for your MATLAB Function Block code, click "Edit Data" to open the Ports and Data Manager, and check the variable-size box for y and give, for example, [513 1] as the upper bound/size. Doing this alerts Simulink to the fact that the data should be variable-size.
If the size of the variable does not change during run-time:
In case you do not want to have variable-size signals in your model, and the size of your data does not change during run-time and is known at compile-time, consider the following approach:
Define a parameter for your MATLAB Function Block by clicking Add > Data in the Ports and Data Manager. Select 'Parameter' as scope, and make sure that the 'Tunable' box is not checked.
You can use this constant parameter now to define the variable sizes at compile-time, by creating a variable of the same name in the MATLAB or model workspace or in the workspace of a masked subsystem containing this MATLAB Function Block. Then, you will be able to use code such as the following in your MATLAB Function block:
>> out = zeros(small,1);