SIMULINK: How to handle cell-array as function return value during codegen

2 views (last 30 days)
Hello,
during codegeneration I get following error:
Unable to determine that every element of 'params{:}' is assigned before this line.
The function building a cell array and returning it works like this:
function data = foo(n)
%[...]
% Init cell arrays
assert(n>=0, 'Num of array elements cant be zero or less.');
data = cell(n+1, 1);
for i=1:n
data{i} = repmat(char(0), 1, len);
coder.varsize('data', [30, 1]);
end
%[...]
The return value of the function is receive as followed:
% [...]
params = cell(cnt, 1);
for j=1:cnt
params{j} = repmat(char(0), 1, len);
coder.varsize('params',[30,1]);
end
params = foo(n);
%[...]
Problem now is, the moment I try to access the values returned by my function 'foo()', an error is beeing reported.
Eaxmple:
P.config.numSlopes = real(str2double(params{2}));
I'm fairly new to codengeneration from Simulink and handling cellarrays, is there something obvious I might have overlooked?

Answers (1)

Harsha Priya Daggubati
Harsha Priya Daggubati on 24 Jul 2019
Hi,
I understand that you are facing an issue with cell arrays, this is because you are using variable cell arrays which you need to be very careful with, just make sure that all the elements of cell array ‘params’ are initialized before code generation, which your error is clearly pointing out.
Refer to the detailed documentation link in which your issue is explained and resolved and try reproducing the same.
Hope this helps you!

Community Treasure Hunt

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

Start Hunting!