MatLab – Palm Chapter 3, Part 2 Cell Arrays, Structure Arrays

Download Report

Transcript MatLab – Palm Chapter 3, Part 2 Cell Arrays, Structure Arrays

MatLab – Palm Chapter 2, Part 3
Cell Arrays, Structure Arrays
Class 7.1
Chapter 2: Sections 6 & 7
Announcements



Departmental Presentations.
(REQUIRED ACTIVITY)
10/12 7:00-9:00 p.m.
See online Schedule
You must turn in two, one-page critiques
(LIMIT EACH CRITIQUE TO ONE PAGE!), one
for each presentation that you attend.
Due: Thursday, October 21, 2004.
Presentation Make-Up

If you have a legitimate conflict:




Go to your conflicting class.
Write a critique of the lecture/lab following
the format below (two pages if course
conflicts with both sessions of
presentations) .
On your cover sheet, give the course,
section, and time of your conflict.
Non-course-related conflicts REQUIRE
instructor approval.
Critique format

Introductory paragraph. Start with
broad statement. Become more
specific with sentences that define the
engineering field you heard about.
End with the topic sentence for your
critique.
Critique format, continued.
Example Introduction:
“Civil engineers design, build and maintain our
nation’s infrastructure. They take courses in structures,
water resources, geotechnical engineering, construction
management, and environmental engineering. These
courses prepare them to work in large or small firms
that are both governmental or private. Although there
are specific areas that CE majors can focus in, many
students choose the “General Track,” which requires
them to take electives in each of the major areas within
civil engineering. Because of its breadth, civil
engineering is an interesting field, and the presenters
for the department were very effective at
communicating their enthusiasm for this subject.”

Broad
Specific
with
definition
Topic
Sentence
Critique format, continued.

Two to three supporting paragraphs.
These paragraphs flesh out your topic
sentence. They MUST contain




A transition from the previous paragraph
A key sentence (may be part of transition)
Supporting sentences
A concluding sentence
Critique format, continued.
Example supporting paragraph:
“Enthusiasm may be hard to define, but you know it
Transition
when you see it. For instance, the geotechnical presenter
had positive body language and used many personal
Key
sentence examples to explain the topic. She talked clearly, in an
animated voice, using a lot of hand gestures. She also
had energy and showed by her facial expression that she
was happy to be there and to have the opportunity to talk
Supporting to our class. One example that stood out in my mind was
sentences how geotechnical engineers have to ensure that
neighboring buildings are not damaged when a new
facility is constructed. The picture of the subway pit right
next to a major high-rise hotel brought that message
home. Because of the active and interesting presentation
Conclusion
style, this presentation kept my attention.”

Critique format, continued.

Conclusion paragraph. Should
summarize the body of the critique and
draw the important conclusions. Should
NOT state substantially new material.
Critique format, continued.
Example conclusion paragraph:
“Through these many techniques, the Department of Civil
Transition
Engineering made a strong presentation. Each presenter
showed excitement, both through energy, inflection, and
personal example, and through a communicated, genuine
Summary
desire to inform us about their topic and to teach us. They
of
supporting were courteous in the way they treated late-comers and
paragraphs loud students, while not letting the presentation get out of
control. They were also knowledgeable and able to
answer questions. Hence, since this presentation was
well-prepared and effectively delivered, I was encouraged
Final
conclusion that it was a beneficial use of my time.”

Critique Style


Each paragraph should be about the
same length.
Sentences should vary in length.
Critique Details

Details (see example):







8 ½” x 11” paper, 1.5 times line spacing.
One-inch margins on all sides.
Times New Roman font.
12pt, bold title (maximum of two lines)
11pt body paragraphs.
Underline: Topic sentence, each key sentence,
and your final conclusion sentence.
Turn in three pages stapled
(1) cover sheet with personal information, (2)
critique #1, (3) critique #2.
Announcements, cont.


Next Monday will be Case Studies, led
by an industry sponsor (e.g. Dell, IBM,
Frito Lay, Motorola, etc.)
We may have handouts on Thursday,
but make sure you arrive on time, and
are courteous, attentive, and ready to
work.
Team Exercise

Budgetary estimate:
How much will it cost each firm to give the
case studies next week? Here is what we
know:




Each team will be on campus for one full day.
Each team is two to four people.
Case studies require (?) hours of extra preparation
by (?) numbers of workers.
Travel is from within Texas.
Budgetary Estimate
Labor estimate:
Hourly Rate:
$130.00
Number of people:
3
Person-hours on campus:
24
Person-hours en route:
24
Person-hours preparation:
40
Total labor: $11,440.00
Organization (copies, etc.):
$291.00
Travel estimate:
Travel round-trip from Dallas/Ft. Worth:
Food (per deum):
Hotel:
$192.00
$315.00
$240.00
Total:
$12,478.00
These costs are paid by the companies as part of their outreach/promotional
budgets. This is probably a LOW estimate.
Team Exercise (Adapted from
Palm Ch. 3, No. 10, p. 178)

An object thrown vertically with a speed v0
reaches a height h at time t where:
1 2
h  v0t  gt
2


Write a function to compute h given v0 and t. It
should allow t to be a vector.
Use the function to PLOT (refer to p. 25) h versus
t for t between 0 and 10 s and v0 = 50 m/s.
Function file (velocity.m)
function [h] = velocity(v0, t)
%
% function [h] = velocity(v0, t)
%
% This function computes the height h of an object at the time t after it is
% released. The initial speed of the object is v0.
% INPUTS:
% v0 = initial speed (m/s)
% t
= time after release (s)
% OUTPUTS:
% h
= height (m)
%
% S. Socolofsky
% ENGR 111A: 501-503
% October 12, 2004
% Define the acceleration of gravity
g = 9.81; % m/s
% Compute the height
h = v0 * t - 1/2 * g * t.^2;
% ^ "dot" not |
^ allows t to be a vector.
% needed
|
Solution
>>
>>
>>
>>
>>
>>
>>
>>
v_init = 50;
t = 0:0.1:10;
h = velocity(v_init, t);
plot(t, h)
xlabel('Time after release (s)')
ylabel('Height above release (m)')
title('Position of a vertically released object')
grid on
Lec. 7.1 Learning Objectives




Understand the differences between vector,
matrix, and structure arrays.
Understand the definitions for structure,
record, and field.
Be able to create a structure array using at
least two different methods.
Be able to store, retrieve, and manipulate
data in the different fields of a structure
array.
Cell Arrays



A Cell Array is an array where each
index is also an array.
Each index can be a different data type
(i.e. you can have letters in one index
and numbers in another).
Allows you to give one name to a set of
data that belong together.
Cell Arrays, continued.

You can create Cell Arrays in two ways.




Cell Indexing: e.g. A(1,1) = {‘Walden’}
Content Indexing: e.g. A{1,1} = ‘Walden’
Note the functions in Table 2.6-1 (p. 112)
celldisp() and cellplot() are useful
commands.
Cell Practice

Type the following commands:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
A = {[1:4], [0, 9, 2], [2:5], [6:8]}
celldisp(A)
cellplot(A)
A{1,3}
A(1,3)
A(1,2)
A{1,2}
A{1,2}(1,2)
A(1,2)(1,3) % why it this an error?
A{1,2}(1,3) % why does this work?
New Topic: Structure Arrays



A structure array is a collection of records.
A record is a set of related fields where each
field may contain a different data type.
A field is an array of data that defines a
particular attribute of an object.
structure array = employee; record = employee(3); List of fields:
name, address, date of employment, salary.
or
The collection of fields comprise a record; the collections of records
comprise a structure array.
3.7 Structure Arrays

Structure Arrays are like data objects
in other languages.




Structure(record).Field1 = [ vector ]
Structure(record).Field2 = ‘string’
Structure(record).Field3 = number
Fields can be arrays, character strings
or numbers.
Structure Arrays


By definition:
“Structures are multidimensional MATLAB
arrays with elements accessed by textual
field designators.”
Examples


Student.name
Student(2).scores(3)
%Data Type = Text
%Data Type = Array of numbers.
Creating Structures

Structures can be created dynamically by direct assignment
to individual fields.


array_name(index).field_name = field_value
An entire record (element of the array) can be added with a
single statement.


Clue(n) = struct(‘field name 1’,field-value-1, ‘field name 2’, field-value-2,
….)
When text values are entered for a field they must be enclose in single
quotes.
Note:
If there is only one record in a structure, there is no index needed
between the array name and the field name. If more than one record
exist then the index value must me given.
Sample of Direct Assignment
%Array names and fields can be created and
%assigned dynamically.
Clue.who = 'Prof Plum';
Clue.where = 'Study';
Clue.what = 'Candlestick';
disp(Clue)
who: 'Prof Plum'
where: 'Study'
what: 'Candlestick'
More Samples
%Even though no index was used in creating the
%first record in 'Clue', another set can be appended
%by using an index.
Clue(2).who = 'Ms. Scarlet';
Clue(2).where = 'Library';
Clue(2).what = 'Rope';
disp(Clue(2))
who: 'Ms. Scarlet'
where: 'Library'
what: 'Rope'
Adding Fields
%New fields can be added after structures
%have been created.
Clue(2).turns = 15; % ‘turns’ is a new field
disp(Clue(2))
% used 1st in record #2
who: 'Ms. Scarlet'
where: 'Library'
what: 'Rope'
turns: 15
Adding Fields
%Adding new fields will cause a 'null' field in the
%cells already defined.
disp(Clue(1))
who: 'Prof Plum'
where: 'Study'
what: 'Candlestick'
turns: [] %turns was not defined originally
The Array Function: struct()
%All fields (a complete record) can be assigned with one
%statement. All text values are enclosed in quotes;
%numerical data are not.
Clue(3) = struct('who', 'Col Mustard',
'where', 'kitchen', 'what', 'revolver',
'turns', 22);
disp(Clue(3))
who: 'Col Mustard'
where: 'kitchen'
what: 'revolver'
turns: 22
Using Array Functions

What does this code segment do?
Clue(1).turns = 17;
avg_turns=sum([Clue.turns])/length(Clue);
disp(avg_turns)
18
Using Array Functions
% Since ‘Clue(1).turns’ is null, [ ], we can assign it a value.
Clue(1).turns = 17;
%Below, notice the lack of an index for Clue and the square brackets
%around Clue.turn. This combination returns a vector of 'turns' to the
%MATLAB function, sum, which calculates the sum of that vector.
%The length function returns the number of records in Clue.
%Dividing the sum by the number of records gives us the average
%of the numbers stored in turns.
avg_turns = sum([Clue.turns])/length(Clue);
%Then we display our results, show 18 as the average.
disp(avg_turns)
18
Try This!
1.
2.
Use MATLAB to create a structure similar to
the Clue Game Database in the examples.
Try these commands in MATLAB




3.
Clue.who
{Clue.who}
[Clue.who]
char(Clue.who)
Clue
Clue(3).turns
[Clue.where]
Clue(2).who
Notice the different ways that the same
data are presented.
See a list of structure related function in Table 2.7-1 on
page 120 of the text.
RAT 7.1




Take out a piece of paper, write your
name, team#, today’s date and RAT
7.1.
A structure array called profs has a
field named college. How would you
assign the value ‘Engineering’ to
this field for record number 2?
You don’t have to turn in your paper
Answer:
>>profs(2).college = ‘Engineering’
Assignment 7.1




INDIVIDUAL ASSIGNMENT
Due: March 9, 2004
Chapter 2; #49, 51 (End of chapter
problems)
Test your understanding problems
2.7-1,2,3 (p. 123)