MATLAB Answers

Kai
0

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

Asked by Kai
on 17 Jul 2019
Latest activity Answered by Harsha Priya Daggubati on 24 Jul 2019
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?

  0 Comments

Sign in to comment.

1 Answer

Answer by 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!

  0 Comments

Sign in to comment.