Generate C/C++ Strings from MATLAB Strings and Character Row Vectors - MATLAB & Simulink (original) (raw)
Main Content
By default, MATLABĀ® strings and character row vectors are mapped to C/C++ character arrays in the generated code. To generate C/C++ strings from MATLAB strings or character row vectors, the MATLAB string or character row vector must be null-terminated (end with zero,0
). For example, the string "Hello World"+char(0)
and character row vector ['Hello World', 0]
are null-terminated.
If a MATLAB string or character row vector is not null-terminated, for example 'Hello World'
, the MATLAB string is mapped to character arrays { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' }
in the generated C/C++ code.
In MATLAB, consider this function:
function t = CharArrayNullAtEnd() t = ['Hello World',0]; end
The corresponding C/C++ code generated for this function is:
void CharArrayNullAtEnd(char t[12]) { int i; static const char cv[12] = "Hello World"; for (i = 0; i < 12; i++) { t[i] = cv[i]; } }
Generating C/C++ strings instead of character arrays improves the readability of the generated code.
Note
If the length of the characters is less than the LoopUnrollThreshold
, a double quoted C/C++ string is not generated in the code even if it is null-terminated. Instead, the code generator produces a C character array that has individual character assignments. By default, the assigned value to LoopUnrollThreshold
is5
. For more information on loop unrolling, see Unroll for-Loops and parfor-Loops.
Add New Line to Strings in Generated Code
When you generate C/C++ strings from null-terminated MATLAB strings or a character row vector, use the newline
function in the MATLAB string or character row vector. The code generator mapsnewline
function to newline character '\n'
in the generated code. If you use the character '\n'
in the MATLAB code instead, it is escaped and is mapped to '\\n'
in the generated code.
In MATLAB, consider this function:
function StringNewline()
string1 = ['Hello World' 0];
string2 = ['My MATLAB' 0];
formatSpecifier = ['%s' newline 0];
coder.cinclude('<stdio.h>');
coder.ceval('printf',coder.rref(formatSpecifier),coder.rref(string1));
coder.ceval('printf',coder.rref(formatSpecifier),coder.rref(string2));
end
The corresponding C/C++ code generated for this function is:
void StringNewline(const emlrtStack *sp) { static const char_T formatSpecifier[4] = "%s\n"; static const char_T string1[12] = "Hello World"; static const char_T string2[14] = "My MATLAB"; (void)sp; printf(formatSpecifier, string1); printf(formatSpecifier, string2); }
In the MATLAB function StringNewline
, if formatSpecifier
is '%s\n'
instead of ['%s' newline 0]
, then the character '\n'
is escaped and you have {'\\','n'}
in the generated C/C++ code.
Limitations
A MATLAB character row vector that has multiple nulls, for example ['Hello', 0, 0]
, is not supported for C/C++ string generation.
See Also
codegen | coder | coder.config | newline | coder.rref | coder.ceval
Topics
- Code Generation for Strings
- Loop unrolling threshold (Simulink Coder)