# 14.2.1: struct Examples and Exercises

• • Carey Smith
• Oxnard College
$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

By Carey A. Smith

##### Example $$\PageIndex{1}$$ Structure for simulation of the effect of an earthquake

% The magnitude 6.7 Northridge, California, earthquake of 17 January 1994

% https://pubs.er.usgs.gov/publication/70017610
Earthquake.name = 'Northridge';
Earthquake.year = 1994;
Earthquake.mag = 6.7;
Earthquake.length = 15; % km
Earthquake.vertical = 70; % cm max vertical movement
% https://en.Wikipedia.org/wiki/1994_N...dge_earthquake
Earthquake.duration = 20; % seconds
Earthquake.accel_g =1.82; % g
Earthquake.accel_ms2=Earthquake.accel_g*9.81; % m/s^2
% Simulation parameter
Earthquake.scale = 1.4; % Scale factor for simulations

.

##### Exercise $$\PageIndex{1}$$ Basketball Team
1. Create a struct for a team of 5 basketball players. They may be men or women, real or fictional. For each member of the team create struct called "player".
2. Create the following fields in the "player" structure for each player.:
1. Name (a string)
2. Position (Choose one of these ‘Center’, ‘Power Forward’, ‘Small Forward’, ‘Shooting Guard’, ‘Point Guard’)
3. Height in inches (not feet) Example: 75
4. Shirt number
3. Fill in you teams structure array with a separate sub-structure for each of the 5 starting players.
4. Example. Suppose you had already filled out the structures for the 1st 4 players. Then the 5th substructure could be defined as follows:

player(5).Name = 'Kareem';
player(5).Position= 'Center';
player(5).Height = 84;
player(5).Shirt = 33;

1. Create a “for loop” that displays each player’s information on one line. Each field should be separated by a tab “\t” or by 4 spaces. Use \n at the end of the format string to put a new line after each player's info.

Example:

fprintf(' Name Position Height Shirt\n') % \n creates a "new line"

for n=1:5

fprintf('%13s \t%14s \t%i \t %i\n', ... % \t inserts a tab

player(n).Name, player(n).Position, ...

player(n).Height, player(n).Shirt)

end

.

##### Example $$\PageIndex2}$$ Triangle struct and draw

This example creates a triangle struct and uses it to draw 2 right triangles on one figure.

The first routine sets the parameters of each triangle.

The second file is a function that draws a triangle using the parameters in the struct. Only one argument, the struct, is passed to the function. Thus, a struct can group related variables and simplify function calls.

%% Triangle structure demo
clear all, format compact, format shortg; close all; fclose all; clc;
% 1st triangle
Tri1.pos = [-9,-1];
Tri1.width = 18;
Tri1.height= 28;
Tri1.color = 'b';
%% Double-click on a structure in the Workspace to see the details

figure;
draw_triangle(Tri1);
axis equal

%% 2nd triangle
Tri2.pos = [0,4];
Tri2.width = 8;
Tri2.height= 16;
Tri2.color = 'r';

hold on;
draw_triangle(Tri2);

function [] = draw_triangle(Tri)
% Draw a right triangle
x = zeros(1,4); % Need 4 points (1st & last are the same)
y = x;
% 1st point
x(1) = Tri.pos(1);
y(1) = Tri.pos(2);
% 2nd point: to the right, with the same y as point 1
x(2) = x(1) + Tri.width;
y(2) = y(1);
% 3rd point: up, with the same x as point 1
x(3) = x(1);
y(3) = y(1) + Tri.height;
% Last point = 1st point
x(4) = x(1);
y(4) = y(1);
% Debug
x = x
y = y

plot(x,y,Tri.color,'Linewidth',3);

###### Solution

This is the resulting plot: .

##### Exercise $$\PageIndex{2}$$ Rectangle struct and draw

The logic and code for this exercise is similar to the

1. [5 pts] Create 3 rectangle structures that specify the position, size, and color of 3 rectangles with these characteristics:

% Rectangle 1

rect1.pos= [30,20]; % x and y coordinates of the lower left corner

rect1.width = 500;

rect1.height= 400;

rect1.color = 'b';

% Rectangle 2

rect2.pos= [350,300]; % x and y coordinates of the lower left corner

rect2.width = 220;

rect2.height= 250;

rect2.color = 'r';

% Rectangle 3

rect3.pos= [300,250]; % x and y coordinates of the lower left corner

rect3.width = 150;

rect3.height= 400;

rect3.color = 'c';

2. [7 pts] Write a draw_rectangle(rect) function whose uses a rect structure to plot 1 rectangle. Since variables in a function are local, you can use "rect" for the structure name inside the function. You don't need to use rect1, rect2, rect3.

3. [3 pts] Open a figure, use ”hold on;” and plot these 3 rectangles by calling your rectangle plotting function with each of the 3 rectangle structures.

• Helps:
• Like the draw_triangle function, the draw rectangle function needs to create an x vector & a y vector with the coordinates of the corners. In order to get back to the starting point & close the rectangle, you will need to have 5 pairs of (x,y) coordinates, with the last coordinate being the same as the first coordinate.
• The 1st corner is like home plate. It is in the lower-left corner.
• The 2nd corner is like 1st base. It’s x-coordinate is the 1st corner’s x-coordinate + the width of the rectangle. It’s y-coordinate is the same as the 1st corner’s .
• The 3rd corner is like 2nd base. It’s x-coordinate is the same as the 2nd corner. It’s y-coordinate is 2nd corner’s y-coordinate + the height of the rectangle.Figure out the coordinates of 3rd base. The last line goes from 3rd base to home plate.
• To plot each of the 4 lines, the 1st argument of plot() is a vector of the X coordinates.The 2nd argument of plot() is a vector of the Y coordinates. 