Code Generation for Constants in Structures and Arrays
The code generator does not recognize constant structure fields or array elements in the following cases:
Fields or elements are assigned inside control constructs
In the following code, the code generator recognizes that the structure fields s.a and s.b are constants.
function y = mystruct()
s.a = 3;
s.b = 5;
y = zeros(s.a,s.b);
If any structure field is assigned inside a control construct, the code generator does not recognize the constant fields. This limitation also applies to arrays with constant elements. Consider the following code:
function y = mystruct(x) s.a = 3; if x > 1 s.b = 4; else s.b = 5; end y = zeros(s.a,s.b);
The code generator does not recognize that s.a and s.b are constant. If variable-sizing is enabled, y is treated as a variable-size array. If variable-sizing is disabled, the code generator reports an error.
Constants are assigned to array elements using non-scalar indexing
In the following code, the code generator recognizes that a(1) is constant.
function y = myarray()
a = zeros(1,3);
a(1) = 20;
y = coder.const(a(1));In the following code, because a(1) is assigned using non-scalar indexing, the code generator does not recognize that a(1) is constant.
function y = myarray()
a = zeros(1,3);
a(1:2) = 20;
y = coder.const(a(1));
A function returns a structure or array that has constant and nonconstant elements
For an output structure that has both constant and nonconstant fields, the code generator does not recognize the constant fields. This limitation also applies to arrays that have constant and nonconstant elements. Consider the following code:
function y = mystruct_out(x) s = create_structure(x); y = coder.const(s.a); function s = create_structure(x) s.a = 10; s.b = x;
Because create_structure returns a structure s that has one constant field and one nonconstant field, the code generator does not recognize that s.a is constant. The coder.const call fails because s.a is not constant.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)