Amin Ya
Amin Ya

Reputation: 1958

Create constant for specifying array size in MATLAB Coder

How can I create a constant variable in MATLAB (and its results the generated C code), so I can use it later in my code to specify the size of variables.

I want to have an array that its size is not hardcoded via number all over the code. I want to specify the size at the beginning of the code like how we do in C code using one of the followings:

const int arraySize=5

#define arraysize 5

Later: int array[arraySize];


When I write the following in MATLAB, Coder just replaces arraySize with the actual number which is 5:

arraySize=int8(5);
array=zeros(1,arraySize); % zeros is just used for specifying size 

Generated code:

void coder(double A[5])
{
  memset(&A[0], 0, sizeof(double) << 16);
}

I tried using the following but it does not allow me to use arraySize in MATLAB calculations:

arraySize=coder.opaque('const int16','5');
A=zeros(1,arraySize);

This may be related to constant folding which I can not disable!


This array size may be repeated throughout the different functions and code many times, so global probably may be related to this

Upvotes: 0

Views: 279

Answers (1)

David Fink
David Fink

Reputation: 126

Having a constant variable appear by name (rather than the value) in the sizes of other variables is unfortunately not supported in MATLAB Coder as of MATLAB R2019a. We've made an internal note of your request so we can look at lifting that limitation in the future.

Upvotes: 3

Related Questions