Unable to Determine That Every Element of Cell Array Is Assigned
Issue
您会看到以下消息之一:
Unable to determine that every element of 'y' is assigned before this line.
Unable to determine that every element of 'y' is assigned before exiting the function.
Unable to determine that every element of 'y' is assigned before exiting the recursively called function.
原因
For code generation, before you use a cell array element, you must assign a value to it. When you usecell
to create a variable-size cell array, for example,cell(1,n)
, MATLAB®assigns an empty matrix to each element. However, for code generation, the elements are unassigned. For code generation, after you usecell
要创建一个可变大小的单元格数组,必须在使用单元格数组之前分配单元格数组的所有元素。
The code generator analyzes your code to determine whether all elements are assigned before the first use of the cell array. The code generator detects that all elements are assigned when the code follows this pattern:
功能z = CellVarSize1D(n, j)%#codegenassert(n < 100); x = cell(1,n);fori = 1:n x {i} = i;endz = x{j};end
这是多维单元阵列的模式:
功能Z = Cellassign3d(M,N,P)%#codegenassert(m < 100); assert(n < 100); assert(p < 100); x = cell(m,n,p);fori = 1:mforj =1:nfork = 1:p x{i,j,k} = i+j+k;endendendz = x{m,n,p};end
If the code generator detects that some elements are not assigned, code generation fails. Sometimes, even though your code assigns all elements of the cell array, code generation fails because the analysis does not detect that all elements are assigned.
Here are examples where the code generator is unable to detect that elements are assigned:
Elements are assigned in different loops
。。。x = cell(1,n)fori = 1:5 x {i} = 5;endfori = 6:n x{i} = 7;end。。。
The variable that defines the loop end value is not the same as the variable that defines the cell dimension.
。。。x =单元格(1,n);m = n;fori = 1:m x{i} = 2;end。。。
For more information, see通过使用单元格的定义可变大小的单元格数组。
Solution
Try one of these solutions:
使用公认的模式分配元素
If possible, rewrite your code to follow this pattern:
。。。x =单元格(1,n);fori = 1:n x {i} = i;endz = x{j};。。。
Userepmat
Sometimes, you can userepmat
to define the variable-size cell array.
考虑定义可变大小单元格数组的代码。它将值1分配给奇数元素,并将值2分配给偶数元素。
功能z = repDefine (n,j)%#codegenassert(n < 100); c =cell(1,n);fori = 1:2:n-1 c{i} = 1;endfori = 2:2:n c{i} = 2;endz = c{j};
Code generation does not allow this code because:
More than one loop assigns the elements.
The loop counter does not increment by 1.
Rewrite the code to first usecell
创建一个1 x-2单元格数组,其第一个元素是1,其第二个元素为2。然后,使用repmat
to create a variable-size cell array whose element values alternate between 1 and 2.
功能z = repVarSize(n, j)%#codegenassert(n < 100); c = cell(1,2); c{1} = 1; c{2} = 2; c1= repmat(c,1,n); z = c1{j};end
通过使用repmat
。使用以下模式:
功能x = emptyVarSizeCellArray x = repmat({'abc'},0,0); coder.varsize('x');end
This code indicates thatx
is an empty, variable-size cell array of1x3
characters that can be passed into or out of functions.
Use编码器
As a last resort, you can use编码器
to indicate that the code generator can allocate the memory for your cell array without initializing the memory. For example:
功能z = nulcpyCell(n, j)%#codegenassert(n < 100); c =cell(1,n); c1 = coder.nullcopy(c);fori = 1:4 c1{i} = 1;endfori = 5:n c1{i} = 2;endz = c1{j};end
Use编码器
with caution. If you access uninitialized memory, results are unpredictable.