Using hetrogenous cells for code generation

7 views (last 30 days)
Arwel
Arwel on 5 Nov 2018
Answered: Ryan Livingston on 5 Nov 2018
Hi,
I am trying to convert some code to C using matlab coder. I've hit a problem with cell arrays which I can't quite see how to solve.
It's easiest to explain the problem with a 'toy' example. If a make a small test function something like this...
function out = cellExample(nParams)
myCell = cell(nParams,4);
for i = 1:nParams
myCell{i,1} = 'test1';
myCell{i,2} = 'test2';
myCell{i,3} = 3;
myCell{i,4} = 4;
end
out = 'ok';
end
..when I run this from matlab, it makes an array that looks like this..
K>> myCell
myCell =
3×4 cell array
'test1' 'test2' [3] [4]
'test1' 'test2' [3] [4]
'test1' 'test2' [3] [4]
But, when I try to convert this using coder, it falls over with the following error....
"This assignment writes a 'double' value into a 'char' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches."
However, I want this to be a hetrogenous cell array, with doubles in some elements, and 'char' in others. How can I do this in coder?
Cheers, Arwel

Answers (1)

Ryan Livingston
Ryan Livingston on 5 Nov 2018
You can do this assuming that nParams is a compile-time constant. For example:
codegen cellExample -args coder.Constant(4) -config:lib -report
works just fine for me. If you are calling cellExample from another function inside of your MATLAB code, then the input must be constant. You can use the following:
function out = cellExample(nParams)
coder.const(nParams); % Give an explicit error if nParams is not constant
myCell = cell(nParams,4);
...
end
to force nParams to be constant and error out if it is not.
The documentation:
talks about other requirements for using heterogeneous cell arrays. Mentally I think of them like structs with convenient indexing. This means that the number of elements and the indexing (like field access) must be constant. Homogeneous cell arrays are more analogous to primitive numeric arrays in that they can be variable size and support non-constant indexing.

Community Treasure Hunt

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

Start Hunting!