sizesimulinklocal-variablespre-allocation

Variable size element in embedded function but fixed input and output


I have a fixed input and output for my simulink embeded function. However I would like to compute a variable size element inside the function, (only used for calculation).

Therefore I would prefer not to declare the block as receiving or sending dynamic size signal. (or using coder.varsize)

ex:

  K =  find( p_var(:) == 0 );     % p_var is a vector containing some zeros
  p_var(K) = [];                  % p_var is a therefore a varsize vector 
                           % the variability is something I need for reason

  n = length(p_var)               % n is dynamic

  M = ones(10,n)                  % M & L are also dynamic
  L = ones(n,50)

  G = M*L;                        % G is fixed of size [10*50]

Here the variable G is always fixed... but I have this type of error :

 Dimension 2 is fixed on the left-hand side but varies on the right ([1 x 9] ~= [1 x :?])

Thank you for your time.


Solution

  • You have to define an upper bound for the size of p_var. This can be done in a couple of ways, such as using coder.varsize as shown below.

    A couple of other things to note:

    1. If p_var is an input to your function you cannot change its size,but would need a temporary variable as shown below.

    2. You most likely do not want to use find as you have done, but should use logical indexing instead.

      function G = fcn(u)

      p_var = u;

      coder.varsize('p_var', [10,1]);

      p_var(p_var(:) == 0) = []; % p_var is therefore a varsize vector the variability is something I need for reason

      n = length(p_var); % n is dynamic

      M = ones(10,n); % M & L are also dynamic L = ones(n,50);

      G = M*L; % G is fixed of size [10*50]