Main Content

na_0021: Strings in MATLAB functions

Since R2020a

Guideline Publication

Control Algorithm Modeling Guidelines - Using MATLAB®, Simulink®, and Stateflow®

Sub ID Recommendations

  • NA-MAAB — a

  • JMAAB — a

MATLAB Versions

All

Rule

Sub ID a

Assignment statements for strings shall not be used in MATLAB functions.

Custom Parameter

Not Applicable

Example — Incorrect

An assignment statement for strings is being used in the MATLAB function.

function y = fcn(u)
%#codegen

str = 'A';

    for i = 1:u
        str = [str 'B'];    
    end

    if strcmp(str, 'ABB')
        y = int16(1);        
    else
        y = int16(0);    
    end
end

Rationale

Sub ID a:

  • MATLAB functions store strings as character arrays. As a result, storing strings of different lengths in the same variable does not support dynamic memory allocation, which prevents the strings from being stored.

    Consider using enumerated types when a string is used in a Switch Case block

Verification

Model Advisor check: Check usage of character vector inside MATLAB Function block (Simulink Check)

Last Changed

R2020a

Version History

Introduced in R2020a