필터 지우기
필터 지우기

Why do I get the error "Non-constant indexing into a heterogeneous cell array is not supported in code generation" when my function takes a cell array input with character vectors of varying length?

조회 수: 23 (최근 30일)
I use MATLAB R2023a and I have a cell array with character vectors of varying length in the base workspace:
n=1000;
cellArray = cell(1,n);
for i=1:n
cellArray{i} = sprintf('String %d',i);
end
I am trying to read this cell array into my Simulink model using a MATLAB Function block with this code below, where the index 'idx' is a block input (runtime variable) and 'out' will be an output string signal:
function [out] = fcn(idx, cellArray)
out = string(cellArray{idx});
end
But I get this error when I try to run my model:
Non-constant indexing into a heterogeneous cell array is not supported in code generation.
The same error will occur if I try to generate code from a MATLAB function with this code using MATLAB Coder.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2023년 11월 13일
편집: MathWorks Support Team 2023년 11월 13일
This is a current limitation of the underlying code generation functionality. Runtime indexing into a heterogeneous cell array is not supported as mentioned in the documentation below:
To work around this, create a local copy of the 'cellArray' variable as shown in the code below:
function [out] = fcn(idx, cellArray)
c = cellArray;
out = string(c{idx});
end
The function input cannot be changed from heterogeneous to homogeneous, while the local copy can.
The "loadStringsFromCellArray.zip" file attached above contains the example and solution.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by