5

Some functions requires the input to be a constant, when run in Matlab Coder. I wish to find a way to declare the input as a constant before it is input as an example for the problematic situation:

  function foo = subsubfunction(x,y)            
      [B,A]=butter(1,x/y);

This will return the error 'All inputs must be constant'

How do I declare x and y as constants so that butter() gets happy? I have tried many solutions and unfortunately not found anything really satisfying. If the command line operation coder.newtype('constant',x) could be used it would simplify everything.

Lokesh A. R.
  • 2,326
  • 1
  • 24
  • 28
Michael Ward
  • 213
  • 5
  • 12
  • do you really mean constant or just one value and not a vector/array ? you could use the `numel` function... – Smash Mar 02 '12 at 15:55
  • Hello and thanks for the answer! But how do you mean that I could use `numel` to solve this problem? In which way will that ensure matlab that the input is constant? – Michael Ward Mar 02 '12 at 16:28

1 Answers1

2

Use coder.const in the function, so that the function butter knows you are passing a constant input. The documentation is available here.

 function foo = subsubfunction(x,y)            
  [B,A]=coder.const(@butter,1,x/y);

Note: You cannot change the value of x/y in the generated code. You could individually change x and y, but not the ratio of the two numbers.

Lokesh A. R.
  • 2,326
  • 1
  • 24
  • 28
  • The documentation you refer to requires a login, please check whether this one is similar and consider updating the link: http://www.mathworks.nl/help/simulink/slref/coder.const.html – Dennis Jaheruddin Jan 14 '14 at 14:39
  • Does not work for me, please see: https://stackoverflow.com/questions/60202957/matlab-coder-all-inputs-must-be-constant – jpo38 Feb 13 '20 at 07:58