I was able to reproduce the issue faced by you, by creating a simple MATLAB function block inside simulink window:
The function code is:
function u_k = mpc_controller(enable, nu)
u_k = z_opt(idx:(idx+nu-1));
When I simulated the model, it threw the following error:
This error is similiar to that reported by you. Also, you want to cascade a "demux" block at the output of the function block, due to which you cannot check the check-box "variable size". I found a work-around to resolve this error. It also allows you to add a "demux" block in cascade with the output.
ASSUMPTION: As stated by you, the size of "u_k" vector is known and is equal to "
". I have assumed "
" to be 9. Define the size of output signal "u_k" by using "Function argument validation". Refer to the documentation:
You can define output argument validation by adding some lines of code inside your MATLAB function block as demonstrated below. Basically, this defines the size of output signal. (here it is defined as 9, because as I said, I have assumed nu=9).
function u_k = mpc_controller(enable, Nu)
u_k = z_opt(idx:(idx+Nu-1));
After typing the above attached MATLAB code in "MATLAB function" block, I modified the simulink model to include a "demux" block cascaded to its output as shown in below figure:
After simulating the above model, no errors are thrown. Therefore, I found it to be working. I am attaching the model for your reference
I hope you find this information useful !