Docsity

Programming - Unit 5: Structures (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 5: Structures (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 5.

S T R U C T U R E S

P R O G R A M M I N G. A E R O S P A C E E N G I N E E R I N G

MATLAB DATA TYPES REVISITED

Scalars: atomic information, single values In matlab, scalars are 1x1 arrays

Structured information

All data in matlab are arrays

1. S T R U C T U R E S

STRUCTURES

  • A structure is a data type that groups logically related data using data containers called fields
  • Very useful to organize complex information in a program
    • Several logically related variables can be treated as a unit instead of as separate entities
  • Differently from arrays, each element (field) can be of a different data type - Example : store flight information Destination airport

flight Origin airport

Flight number

ACCESSING FIELDS: THE DOT OPERATOR

  • To access a field of a structure, we use
    • the dot operator (.)
    • The field name
      • Differently to arrays where elements are accessed using the index
  • assign values to a field myFlight.origin = 'BCN' myFlight.destination = 'LAS' myFlight.number ='EK499';

myFlight.origin myFlight.destination

EXTENDING A STRUCTURE BY ASSIGNMENT

  • MatLab will extend a structure if a value is assigned to a new field
    • myFlight.airline ='Iberia';
  • Handle with care!
    • Good programming practice is to define structures using struct
    • Extending by assignment will not work in most programming languages

NESTED STRUCTURES

  • Fields of a structure can be variables of any data type:
    • scalar (int, double, char),
    • structured (array or another structure)
  • A structure inside another structure is called a nested structure

Flight details (^) position

ground speed longitude altitude

Destination airport

Origin airport

Flight number

latitude

flight

myFlight.details.position.altitude =11582; myFlight.details.groundspeed=959;

3. A R R AY S A S F I E L D S

O F A S T R U C T U R E

4. A R R AY S O F

S T R U C T U R E S

ARRAYS OF STRUCTURES

  • Vectors or matrixes whose elements are structures
  • Specially useful to store and manage information
    • We seldom use one structures on its own, much more common vector of structures
    • Example: vector of students to store data from a class

students(3).name = 'Bill' students(3).marks(1)= 5.0;

HOW TO BUILD AN ARRAY OF STRUCTURES

  • Option 2. Pre-allocating with cells
    • Assign a cell array to each field

students =struct ('name',cell(1,n),... 'surname',cell(1,n),... 'age',cell(1,n),... 'marks',cell(1,n)... );

ACCESSING DATA IN A ARRAY OF STRUCTURES

  • Brackets to identify the element
  • Dot notation to identify the field

students(4).name

CELL ARRAYS

  • A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data.
  • Similar to an array, but elements are not the same type
  • Cell arrays can be indexed just as standard array
  • Use
    • lists of character vectors of different lengths,
    • mix of strings and numbers, or numeric arrays of different sizes

CELL ARRAY EXAMPLE

myCell = {1,2,3; 'text', rand(5,10,2), [11; 22; 33]}

myCell= 2×3 cell array {[ 1]} {[ 2]} {[ 3]} {'text'} {5x10x2 double} {3x1 double}