# How to create constant(!) input-dependent matrices with a Matlab function block without that variable-size stuff

30 views (last 30 days)
Frank Brütting on 12 Apr 2021 at 17:38
Edited: Frank Brütting on 13 Apr 2021 at 17:22
I need to implement a Matlab function block which creates matrices according to a given size specified by an input signal. The size value is constant(!) throughout a simulation, and I don’t want to use superfluous variable-size stuff, as I don’t want to overcomplicate my simulation and sample the block totally needlessly.
Easy example for the Matlab function: y = zeros(x) with x being the input signal of the Matlab function block and y being the output signal. That input gets connected to a Constant block set to a fixed value of say 5. Then this block should do nothing more than create a 5x5 matrix full of zeros. I am not able to do this without messing the whole simulation up with variable-sized signals and needless block sampling.
I would appreaciate it a lot, if someone can help, me. That already costed me days.
No matter what I do, I always get the error message:
Data 'y' is inferred as a variable size matrix, while its properties in the Model Explorer specify its size as inherited or fixed. Please check the 'Variable Size' check box and specify the upper bounds in the size field.
But that signals is not variable at all, it’s absolutely fixed! So I do not understand at all how Simulink cannot be able to infer the signal size from that constant value.
Block diagram
Matlab function block content
Error message
##### 2 CommentsShowHide 1 older comment
Frank Brütting on 13 Apr 2021 at 11:25
I simplified my problem to a minimal example, in reality I need to transform matrix signals of arbitrary size – which is why the formula in the end would be much more complex and I need to have an input and an output signal. The matrix signal size though is fixed throughout the whole simulation (I need to do lots of simulations, but each with a different signal size), and I want to pass the signal dimensions to the block via a constant expression.
In other programming languages constant expressions get optimized by the precompiler in advance, so I’m searching for the equivalent method here.

Fangjun Jiang on 13 Apr 2021 at 12:35
Edited: Fangjun Jiang on 13 Apr 2021 at 12:37
MATLAB Function block can deal with variable size. It is just a lot of annoynance like you suggested. You need to declare the max size of the variable.
The size of your "y" change per simulaiton but is determined at the begining of the simulation. But does the value of "y" change per simulation step? If not, you can do a "InitFcn" callback to create "y".
If "y" changes value at every simulation step, you can still avoid the variable size issue in MATLAB Function block. I just tried a simple example. Drag a default MATLAB Function block, connect its input to a Constant block whose value is rand(N), connect the output to a "Display" block.
Change the value of N to 2,3,4 everytime you run a simulation. It seems working as you desired.
Frank Brütting on 13 Apr 2021 at 17:21
When I turn on color-coding, I even can see that Simulink knows that the dimensions specified in a Constant block are of constant value, due to it being colored pink! Yet Simulink is bugging me about not being able to infer the size of that constant value. I don’t get this, there seems to be an obvious minor problem in Simulink, it totally should be able to infer the dimension when it’s based off a constant value.
If you can get a developer looking at this, I think this should be relatively easy to fix. Because at some point, Simulink seems to just lose the fact that the value is constant and therefore starts to come up with variable-size.

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!