16.4.1: struct Examples and Exercises
- Page ID
- 85177
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\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]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\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]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)By Carey A. Smith
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
Solution
Add example text here.
.
- 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".
- Create the following fields in the "player" structure for each player.:
- Name (a string)
- Position (Choose one of these ‘Center’, ‘Power Forward’, ‘Small Forward’, ‘Shooting Guard’, ‘Point Guard’)
- Height in inches (not feet) Example: 75
- Shirt number
- Fill in you teams structure array with a separate sub-structure for each of the 5 starting players.
- 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;
-
Create a header with this line:
fprintf(' Name Position Height Shirt\n') % \n creates a "new line"
6. Create a “for loop” that goes from 1 to 5 and 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 for loop. (The tabs and/or spaces could be changed, but the for loop must go from n=1 to n=5)
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
- Answer
-
Create your own struct.
.
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:
.
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.
- Answer
-
The resulting plot should look like this:
Add texts here. Do not delete this text first.
.