Index Substructures and Fields
Use these guidelines when indexing substructures and fields for code generation:
Reference substructure field values individually using dot
notation
For example, the following MATLAB® code uses dot notation to index fields and substructures:
... substruct1.a1 = 15.2; substruct1.a2 = int8([1 2;3 4]); mystruct = struct('ele1',20.5,'ele2',single(100), 'ele3',substruct1); substruct2 = mystruct; substruct2.ele3.a2 = 2*(substruct1.a2); ...
The generated code indexes elements of the structures in this example by resolving symbols as follows:
Dot Notation | Symbol Resolution |
---|---|
substruct1.a1 | Field a1 of local structure substruct1 |
substruct2.ele3.a1 | Value of field a1 of field ele3 ,
a substructure of local structure substruct2 |
substruct2.ele3.a2(1,1) | Value in row 1, column 1 of field a2 of
field ele3 , a substructure of local structure substruct2 |
Reference field values individually in structure arrays
To reference the value of a field in a structure array, you must index into the array to the structure of interest and then reference that structure's field individually using dot notation, as in this example:
... y = X(1).a % Extracts the value of field a % of the first structure in array X ...
To reference all the values
of a particular field for each structure in an array, use this notation
in a for
loop, as in this example:
... s.a = 0; s.b = 0; X = repmat(s,1,5); for i = 1:5 X(i).a = i; X(i).b = i+1; end
This example uses the repmat
function
to define an array of structures, each with two fields a
and b
as
defined by s
. See Define Arrays of Structures for Code Generation for more information.
Do not reference fields dynamically
You cannot reference fields in a structure by using dynamic names, which express the field as a variable expression that MATLAB evaluates at run time (see Generate Field Names from Variables).
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)