主要内容

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}cis 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 =单元格(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不支持异质细胞万博1manbetx阵列。

  • 如果您使用细胞定义固定尺寸的单元格数组的功能,您无法使用coder.varsizeto specify that the cell array has a variable size. For example, this code causes a code generation error becausex = cell(1,3)makesxa fixed-size,1-by-3 cell array.

    。。。x = cell(1,3); coder.varsize('X',[1 5])。。。

    您可以使用coder.varsize使用您使用卷曲支架定义的单元格数组。例如:

    。。。x = {1 2 3};coder.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

    See通过使用单元格的定义可变大小的单元格数组

    To specify upper bounds for the cell array, usecoder.varsize

    功能mycell(n)%#codegenx =单元格(1,n);fori = 1:n x {i} = i;coder.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 {:}”的每个元素。

Sometimes, even though your code assigns all elements of the cell array, the code generator reports this message because the analysis does not detect that all elements are assigned. SeeUnable 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 =单元格(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 =单元格(1,n);m = n;fori = 1:m x{i} = 2;endz = x{j};end

    重写要使用的代码nfor the cell creation and the loop end value:

    功能z = mycell(n,j)x =单元格(1,n);fori = 1:n x{i} = 2;endz = x{j};end

  • 使用此模式创建单元格数组:

    x = cell(1,n)

    Do not assign the cell array to a field of a structure or a property of an object. For example, this code is not allowed:

    myobj.prop =单元格(1,n)fori = 1:n。。。end

    不使用the细胞单元阵列构造函数中的功能{}。例如,不允许此代码:

    x = {cell(1,n)};

  • 单元阵列创建和将值分配给单元阵列元素的循环必须在唯一的执行路径中合在一起。例如,不允许以下代码。

    功能z = mycell(n)如果n> 3 c =单元格(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 usingfor-loops with constant bounds.

    例如,不允许以下代码。

    x = {1,'mytext'};disp(x {randi});

    您可以将索引成一个异质的单元格数组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- 环具有较大的身体或具有许多迭代,展开可以增加编译时间并产生效率低下的代码。

    如果AB是恒定的,以下代码显示了索引到一个异构单元格数组中的索引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}仅使用向量。例如,不允许以下代码,因为Xis 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 becauseXis homogeneous.

    。。。x = {1 2};fori = 1:n x {end + 1} = 3;end。。。

    此代码不允许,因为Xis 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.

相关话题