-1

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.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Chewbaka
  • 184
  • 10

1 Answers1

1

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]

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Thank you ! it works well. I think my error using coder.varsize initially was to try to use directly the input of the function and change its size. (which led to the error about variable input/output) I was a little confused on how to treat memory allocation of varsize element on simulink. – Chewbaka Apr 03 '18 at 09:03