30 views (last 30 days)

Show older comments

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

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.

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

Start Hunting!