Search code examples
matlabglobal-variablesglobalsimulinkdimensions

Setting dimension of signal in Simulink/MATLAB function from workspace


Suppose I have a Simulink block that contains something like:

function y  = myFnc(x, par)

y = zeros(1, par(1)) + x;

I declare par as a Parameter in the model explorer and read its value from the workspace. Now, Simulink infers that y is a variable-size signal with all the pretty nasty limitations associated with it.

Is there any way to parametrize signals dimensions from the workspace, yet leaving the signals fixed-size somehow?

In C/C++ I'd just declare a constant.


Solution

  • What works with some limitations is to create a class with properties that are const like:

        classdef DIMS
            properties (Constant)
                NR_SIGNALS          = 10
                LEN_SIG             = 32000 
                BLOCKS              = 2 * DIMS.NR_SIGNALS
            end
        end 
    

    ... and use the constant properties (e.g. DIMS.LEN_SIG) as parameters wherever applicable.

    The parametrization will be fixed at compile time of the model and even hard-coded in the generated code (if applicable).