Resolve Issue: coder.varsize Not Supported for Strings - MATLAB & Simulink (original) (raw)

Main Content

Issue

Code generation does not support defining string variables as variable-size by using the coder.varsize directive. If you callcoder.varize on a string variable, you see this error message during code generation:

coder.varsize() is not supported for variables of string type.

Possible Solutions

Consider this MATLABĀ® function, which returns a string. Code generation fails for this function because the code generator defines s as a fixed-size string with length 0 and cannot then increase the size of the string during the for-loop. You cannot usecoder.varsize to define s as variable size because s is a string.

function out = varSizeStringError(n) s = ""; for i = 1:n s = s + ":) "; end out = s; end

Avoid Using Strings

If your application does not require strings, you can use a variable-size character vector instead of a string. For example:

function out = varSizeStringExample1(n) s = ''; coder.varsize('s'); for i = 1:n s = append(s,':) '); end out = s; end

Convert Variable-Size Character Vector to String

Because code generation supports defining variable-size character arrays usingcoder.varsize directive, you can define a variable-size character vector and then convert that vector to a string. For example:

function out = varSizeStringExample2(n) chars = ''; coder.varsize("chars"); s = convertCharsToStrings(chars); for i = 1:n s = s + ":) "; end out = s; end

Convert Array of Blanks to String

If you convert an empty array of blanks to a string, you can change the length of this string without explicitly defining it as variable size. For example:

function out = varSizeStringExample3(n) s = string(blanks(coder.ignoreConst(0))); for i = 1:n s = s + ":) "; end out = s; end

See Also

coder.varsize