Cell Array Limitations for Code Generation
什么时候you use cell arrays in MATLAB®用于代码生成的代码,您必须遵守这些限制:
Cell Array Element Assignment
You must assign a cell array element on all execution paths before you use it. For example:
功能z = foo(n)%#codegenC =细胞(1,3);如果n <1 c {2} = 1;别的c {2}= n;endz = c {2};end
代码生成器认为将单元数组传递给函数,或者将其从函数返回它作为小区阵列的所有元素的使用。因此,在将单元格数组传递给函数或从函数返回它之前,必须分配所有元素。例如,不允许以下代码,因为它不会为其分配值c {2}
和c
is a function output.
功能c = foo ()%#codegenC =细胞(1,3);c {1} = 1;c {3} = 3;end
The assignment of values to elements must be consistent on all execution paths. The following code is not allowed becausey {2}
is double on one execution path and char on the other execution path.
功能Y = foo(n)y = cell(1,3)如果n > 1; y{1} = 1 y{2} = 2; y{3} = 3;别的y {1} = 10;y {2} ='a'; y{3} = 30;end
可变大小的单元格阵列
coder.varsize
不支持异质细胞金宝app阵列。如果你使用
细胞
函数要定义固定大小的单元格数组,则无法使用coder.varsize
to specify that the cell array has a variable size. For example, this code causes a code generation error becausex = cell(1,3)
makesx
a fixed-size,1-by-3 cell array.。。。x = cell(1,3); coder.varsize('X',[1 5])。。。
您可以使用
coder.varsize
使用使用Crace括号定义的单元格数组。例如:。。。x = {1 2 3};编码器。varsize('X',[1 5])。。。
To create a variable-size cell array by using the
细胞
功能,使用此代码模式:功能mycell(n)%#codegenx =单元格(1,n);fori = 1:n x {i} = i;endend
To specify upper bounds for the cell array, use
coder.varsize
。功能mycell(n)%#codegenx =单元格(1,n);fori = 1:n x {i} = i;编码器。varsize('X',[1,20]);endend
Definition of Variable-Size Cell Array by Using细胞
For code generation, before you use a cell array element, you must assign a value to it. When you use细胞
例如,创建变量大小的单元阵列,细胞(1,n)
, MATLAB assigns an empty matrix to each element. However, for code generation, the elements are unassigned. For code generation, after you use细胞
要创建变量大小的单元格数组,必须在使用单元格数组之前分配单元数组的所有元素。例如:
功能z = mycell(n, j)%#codegenx =单元格(1,n);fori = 1:n x {i} = i;endz = x {j};end
代码生成器分析代码以确定在第一次使用单元格数组之前是否分配了所有元素。如果代码生成器检测到未分配某些元素,则代码生成失败了错误消息。例如,修改上限的上限for
- 为j
。
功能z = mycell(n, j)%#codegenx =单元格(1,n);fori = 1:j%<- Modified herex{i} = i;endz = x {j};end
使用此修改和输入j
少于n
,该函数不会为所有单元格数组元素分配值。代码生成产生错误:
无法确定在此行之前分配“x {:}”的每个元素。
有时,即使您的代码分配了单元格数组的所有元素,代码生成器也会报告此消息,因为分析不会检测到所有元素已分配。看Unable to Determine That Every Element of Cell Array Is Assigned。
To avoid this error, follow these guidelines:
什么时候you use
细胞
to define a variable-size cell array, write code that follows this pattern:功能z = mycell(n, j)%#codegenx =单元格(1,n);fori = 1:n x {i} = i;endz = x {j};end
以下是多维单元集的模式:
功能z = mycell(m,n,p)%#codegenx =细胞(m,n,p);fori = 1:mforj = 1:nfork = 1:p x{i,j,k} = i+j+k;endendendz = x{m,n,p};end
递增或递减循环计数器
1
。Define the cell array within one loop or one set of nested loops. For example, this code is not allowed:
功能z = mycell(n,j)x = cell(1,n);fori = 1:5 x {i} = 5;endfori = 6:n x{i} = 5;endz = x {j};end
使用相同的变量为单元格尺寸和循环初始值和终端值。例如,代码生成失败了以下代码,因为小区创建使用
n
和循环终端值使用m
:功能z = mycell(n,j)x = cell(1,n);m = n;fori = 1:m x{i} = 2;endz = x {j};end
重写用于使用的代码
n
for the cell creation and the loop end value:功能z = mycell(n,j)x = cell(1,n);fori = 1:n x{i} = 2;endz = x {j};end
使用此模式创建单元格数组:
x = cell(1,n)
Assign the cell array to a field of a structure or a property of an object by initializing a temporary variable with the required cell. For example:
t = cell(1,n)fori = 1:n t{i} = i+1;endmyObj.prop = t;
myobj.prop = cell(1,n);fori = 1:n myobj.prop {i} = i + 1;end
不使用the
细胞
单元格阵列构造函数内的功能{}
。例如,不允许此代码:x = {cell(1,n)};
小区阵列创建和为小区数组元素分配值的循环必须在唯一的执行路径中在一起。例如,不允许使用以下代码。
功能z = mycell(n)如果n> 3 c = cell(1,n);别的c = cell(n,1);endfori = 1:n c{i} = i;endz = c{n};end
To fix this code, move the assignment loop inside the code block that creates the cell array.
功能z = cellerr(n)如果n > 3 c = cell( 1,n);fori = 1:n c{i} = i;end别的c = cell(n,1);fori = 1:n c{i} = i;endendz = c{n};end
单元格数组索引
您无法使用流畅的括号索引单元格数组
()
。Consider indexing cell arrays by using curly braces{}
访问单元格的内容。You must index into heterogeneous cell arrays by using constant indices or by using
for
-loops with constant bounds.例如,不允许使用以下代码。
x = {1,'mytext'};disp(x {randi});
您可以在a中索引到异构单元阵列中
for
-loop with constant bounds because the code generator unrolls the loop. Unrolling creates a separate copy of the loop body for each loop iteration, which makes the index in each loop iteration constant. However, if thefor
-Loop有一个大的身体或它有很多迭代,展开可以增加编译时间并产生低效的代码。如果
A
和B
是常量的,以下代码显示索引到a中的异构单元阵列中for
- 使用常量界限。x = {1,'mytext'};fori = a:b disp(x {i});end
Growing a Cell Array by Using {end + 1}
生长细胞阵列X
, you can usex {end + 1}
。例如:
。。。x = {1 2};x {end + 1} ='a';。。。
什么时候you use{结束+ 1}
to grow a cell array, follow these restrictions:
只使用
{结束+ 1}
。不使用{end + 2}
,{end + 3}
, and so on.采用
{结束+ 1}
只有向量。例如,不允许以下代码,因为X
is a matrix, not a vector:。。。x = {1 2; 3 4}; X{end + 1} = 5;。。。
采用
{结束+ 1}
only with a variable. In the following code,{结束+ 1}
does not cause{1 2 3}
to grow. In this case, the code generator treats{结束+ 1}
作为一个缺乏界限指数X{2}
。。。。x = {'a'{ 1 2 3 }}; X{2}{end + 1} = 4;。。。
什么时候
{结束+ 1}
grows a cell array in a loop, the cell array must be variable-size. Therefore, the cell array must behomogeneous。This code is allowed because
X
is homogeneous.。。。x = {1 2};fori = 1:n x {end + 1} = 3;end。。。
不允许此代码,因为
X
is heterogeneous.。。。x = {1'a'2'b'};fori = 1:n x {end + 1} = 3;end。。。
单元格阵列内容
单元格阵列不能包含mxarrays
。在单元格数组中,您无法存储外部函数返回的值。
Passing Cell Arrays to External C/C++ Functions
您无法通过单元格数组Coder.CEVAL.
。如果一个变量是一个输入基于“增大化现实”技术gument toCoder.CEVAL.
, define the variable as an array or structure instead of as a cell array.