Resolve Error: Code Generation Does Not Support String and Object Arrays
Issue
Code generation does not support object arrays and string arrays. If you use a string or object array in your MATLAB® code, or if you specify a string or object array as an input argument to your entry-point function, you see one of these error messages:
Code generation does not support
object arrays. Use a cell array of className
objects.
Code generation does not support string
arrays. Use a cell array of character vectors.
Code generation does not support arrays
of className objects. Instead of a string array, use a
cell array of character vectors. Instead of an object array, use a cell array of
objects.
Possible Solutions
If your code uses a string array, replace the array with a cell array of character
vectors If your code uses an object array, replace the array with a cell array of
objects. If you use an object array and assign values to object properties by using
nonscalar indexing, use a cell array of objects and the deal function.
Use Cell Array of Character Vectors
If your MATLAB code uses a string array, rewrite your code to use a cell array of character vectors.
For example, consider the function useStrings_error, which
creates a string
array.
function out = useStrings_error(n) %#codegen s = ":)"; for i = 2:n s(end+1) = ":)"; end out = s; end
Code generation for useStrings_error fails because
s is a string array. To resolve this error, rewrite the
function to use a cell array of character vectors. For example, code generation for
the function useCharacters
succeeds.
function out = useCharacters(n) %#codegen s = {':)'}; for i = 2:n s{end+1} = ':)'; end out = s; end
Use Cell Array of Objects
If your MATLAB code uses an object array, rewrite your code to use a cell array of objects.
For example, consider the MATLAB function makeSquares_error, which uses the class
MySquare. Code generation for
makeSquares_error fails because
mySquaresCollection is an object
array.
classdef MySquare properties side end end function out = makeSquares_error(n) %#codegen obj = MySquare; obj.side = 1; mySquaresCollection = [obj]; for i = 2:n obj.side = i^2; mySquaresCollection(end+1) = obj; end out = mySquaresCollection; end
To resolve this error, define mySquares as a cell array. For
example, code generation for makeSquares
succeeds.
function out = makeSquares(n) %#codegen obj = MySquare; obj.side = 1; mySquaresCollection = {obj}; for i = 2:n obj.side = i^2; mySquaresCollection{end+1} = obj; end out = mySquaresCollection; end
Use Cell Array of Objects and deal
If your MATLAB class defines a class constructor method, you can assign values to
that class by using indexed assignment. If you use this coding pattern, you can use
the deal function to perform the same
style of assignment in your MATLAB code for code generation.
For example, consider the class MyConstructorClass and the
function useClass_error that uses the class. The class
MyConstructorClass has a class constructor method that
assigns a value to MyConstructorClass.prop. The
useClass_error function creates an array of
MyConstructorClass objects, and uses indexed assignment to
assign values to a subset of objects in the array. Code generation for
useClass_error fails because myObjs is an
object array.
classdef MyConstructorClass properties prop end methods function obj = MyConstructorClass(val) obj.prop = val; end end end function out = useClass_error(n) %#codegen obj = MyConstructorClass(0); myObjs = [obj obj obj obj obj]; myObjs(2:3) = [MyConstructorClass(n^2) MyConstructorClass(n+2)] out = myObjs; end
To resolve this error without converting the indexed assignment into a loop, use a
cell array and the deal function. For example, code generation
for the function useClass
succeeds.
function out = useClass(n) %#codegen obj = MyConstructorClass(0); myObjs = {obj obj obj obj obj}; [myObjs{2:3}] = deal(MyConstructorClass(n^2),MyConstructorClass(n+2)); out = myObjs; end