MATLAB Answers

Coder - Create constant for specifying array size

조회 수: 20(최근 30일)
amin ya
amin ya 15 Mar 2019
편집: amin ya 16 Mar 2019
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.
In C, we usually do this:
const int arraySize = 5;
int array[arraySize];
When I write the following in the MATLAB, coder just repalces arraySize with the actual number which is 5:
arraySize=5;
array=zeros(1,arraySize); % zeros is just used for specifying size
(This array size may be repeated throughout the different functions and code many times, so global probably may be related to this)
I want to use static memory allocation as long as it is possible.

  댓글 수: 0

로그인 to comment.

답변(1개)

Fangjun Jiang
Fangjun Jiang 15 Mar 2019
Please refer to the document
Code Generation for Variable-Size Arrays
web(fullfile(docroot, 'simulink/ug/what-is-variable-size-data.html'))
Control Memory Allocation for Variable-Size Arrays in a MATLAB FunctionBlock
web(fullfile(docroot, 'ecoder/ug/control-memory-allocation-for-variable-size-arrays-in-a-matlab-function-block.html'))

  댓글 수: 1

amin ya
amin ya 16 Mar 2019
This is not useful.
I want to have an array that its size is not hardcoded via number all over the code. This may be related to constant folding which I can not disable!
I want to specify the size at the beginning of the code like how we do in C code using one of the followings:
constant int arraySize=5
or
#define arraysize 5
--------------------------------------------------------------------------------
For example the generated code for my This example, where a is defined as a global constant int8(5),
function A=coder()
global a
A=zeros(a,a);
end
is this, which you see 5 is used in the code instead of being defined in the beginning
#include <string.h>
#include "coder.h"
void coder(double A[5][5])
{
memset(&A[0][0], 0, sizeof(double) << 16);
}
--------------------------------------------------------------------------------
I tried using the following but it does not allow me to use a in MATLAB calculations:
a=coder.opaque('const int16','5');
A=zeros(a,a);

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

제품


릴리스

R2018b

Translated by