Title: Class Prep
1Class Prep
- Bring to Class
- HW
- In-Class Exercises
- Paper for Classroom Printer
- Copy sample programs from course folder
- Run MATLAB
2Set Current Directory
- Run MATLAB and switch to your sub-folder as your
Current Directory
3Week 05-b(6.1-6.6)
- Mapping Casting
- Character String Operations
4Function Parameters are Call by "Value"
- When you call a function like
- gtgt maxval destruct_o_max(a)
- the parameter a is COPIED into the function
- If you change the value of an input parameter in
a function it does NOT change in the command
workspace. - NOTE You wouldn't want to have sqrt(x) modify
the value of x! - More seriously, what if sqrt(7) modified "7"?
5"Swap" Function e.g. use swap(p,q) in program
- function badswap(x,y)
- temp x
- x y
- y temp
- end
- function out1 out2 swap(in1,in2)
- reverses order of inputs on the outputs
- called by p q swap(p,q)
- out1 in2
- out2 in1
6Good Practices
- Make your scripts as general as possible
- Use a variable for each input
- Ask the user for the input values (with
appropriate prompts) - Label all results
- Meaning
- Units
- Make the results as readable as possible
- Line up columns
- Display values to the correct number of decimal
places - Use white space effectively
7Displaying Concatenations of Values
- gtgt disp('Union ' 'University')
- Union University
- gtgt name 'Sam'
- gtgt disp('Hello ' name)
- Hello Sam
8DEMO input Strings of Characters
- If 's' is provided as the second parameter to
input(), the complete input character sequence
is saved as a string regardless of content. - No need to put string in quotes!
- who input('What is your name? ','s')
- how input('So, ' who ', how are you? ', 's')
9Review How are Characters Stored?
- Character arrays are similar to vectors, except
- Each element contains a single character
Review Conversion between Character and Numeric
gtgt u double(T) double is a built-in
function. gtgt char(u) char performs the
opposite function.
Example
gtgt udouble(C) u 72 101 108 108
111 gtgt char(u) ans Hello
10Hands-On DEMO Manipulating Strings
- alpha 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- alpha(24-116)
- alpha(210)'
- m 'I can''t find the manual!' Note two
apostrophes in string - u 'If a woodchuck could chuck wood, '
- v 'how much wood could a woodchuck chuck?'
- w u v string concatenation
- disp(u)
11String Comparison
- Strings may be compared as vectors however, they
must then obey vector comparison rules - strcmp() and strcmpi() compare strings of any
length. - gtgt 'abcd' 'abcd'
- ans 1 1 1 1
- gtgt 'abcd' 'abcde'
- ??? Error using gt eq
- Array dimensions must match for binary array op.
- gtgt strcmp('abcd', 'abcde')
- ans 0
- gtgt strcmp('abcd', 'abcd')
- ans 1
- gtgt 'abc' 'a'
- ans 1 0 0
- gtgt strcmpi('ABcd', 'abcd') Ignore cAsE
- ans 1
12Hands-On DEMO Comparing chars and strings
- name 'frederick'
- name gt 'g' evals to 1's and zeros
- him1 'fred'
- him2 'george'
- him1 him2 error
- strcmp(him1,him2)
- him3 'fred'
- strcmp(him1,him3)
13Converting Numeric Values to Strings
- WHY?
- Try d 67 ' is prime, so is ' 73
- The num2str() and int2str() functions
- d int2str(16) '-Feb-' int2str(2008)
- disp(d)
- 16-Feb-2008
- x 23.11
- disp( 'answer ' num2str(x) )
- answer 23.11
- disp( 'answer ' int2str(x) )
- answer 23
14Hands-On DEMO Convert Numbers to Strings
- gtgt anum2str(123.45)
- a 123.45 Look in Workspace window at a
- gtgt class(a)
- ans char
- Concatenating strings lets you give units of
measure! - gtgt wgt 234.5
- gtgt disp ('The weight is ' num2str(wgt) '
grams.') - The weight is 234.5 grams.
15Conversion from Strings to Numbers using input
- The function input('prompt ') presents the
string parameter to the user in the Command
window and waits for the user to type some
characters and the Enter key. - Then it converts the input string according to
the following rules. If the string begins with - a numerical character, MATLAB converts the string
to a number - An alphabetic character, MATLAB constructs a
variable name and looks for its definition - an open bracket, '', an array is constructed
- the single quote character, MATLAB creates a
string - If a format error occurs, MATLAB repeats the
prompt.
16disp is not enough!
disp is not able to produce nicely formatted
output.
gtgt fprintf(\nThe value of A is f\n, A) The
value of A is 8.000
The first argument (a string) is called the
template. It contains a mixture of text to be
printed, formatting characters, and output
specifiers. \n is a newline character. f tells
fprintf to print a floating point number (f). For
every specifier in the template, an extra
argument is normally required. These arguments
are the values to be printed.
17Simple Formatted Output
- fprintf function
- Syntax fprintf( format string, var1, var2, )
- Examples
- fprintf('You weigh f pounds.', weight)
- fprintf('You weigh f pounds and you drank f
beers.', weight, numBeers)
18Special Formatting Symbols
- The fprintf( format, data ) function
- d integer (d for decimal)
- f floating point format
- e exponential format
- g general purpose (d, e or f depending on value)
- s string of characters
- Optional Width and Precision
- w.p
- w total width of field (0 for minimal
neededexpands) - .p number of places after decimal (for f, e,
g only) - Place between and d/f/e/g (e.g. 8.2f)
19Special Format Commands
- \n ? new line We'll use this most often
- For fprintf only
- (We will use \newline in titles labels of
plots) - \r ? carriage return (no line feed)
- \t ? tab
- \b ? backspace
20Hands-On DEMO fprintf( ) of Scalar(use the
ltup-arrowgt key to change just the format)
temp 98.632 fprintf('The temperature is 8.1f
degrees F.\n', temp)
The temperature is 98.6 degrees F.
fprintf('The temperature is 08.2f degrees F.\n',
temp)
The temperature is 00098.63 degrees F.
fprintf('The temperature is 10.3e degrees F.\n',
temp)
The temperature is 9.863e001 degrees F.
21Displaying Data Details...
- fprintf('Area of a circle with radius d is
f\n',3, pi32) - Area of a circle with radius 3 is 28.274334
- defaults to 6 decimal places in f format
- x pi y13.42
- fprintf( 'x 0.2f \n', x ) 0
for width expands to fit value - x 3.14
- fprintf( 'x d\ny d\n', pi, y )
multiple lines, same fprintf - x 3y 13
22Hands-On DEMO Vector Output
- What happens when we use fprintf with a vector?
- Reuses format if runs out of slots for values
- count 10-10
- fprintf('Countdown d\n\n', count)
23fprintf( ) of Matrices
Score 1 2 3 4 75 88 102 93 99 84 95
105
fprintf('Game 1.0f score Houston 3.0f
Dallas 3.0f \n',Score)
Game 1 score Houston 75 Dallas 99 Game
2 score Houston 88 Dallas 84 Game 3
score Houston 102 Dallas 95 Game 4
score Houston 93 Dallas 105
NOTE Matrix is traversed column wise
24HW 5 User-Defined Functions Strings
- 5a) Bungee Jumper
- 5b) Problem 8, page 130.
- 5c) Problem 17, page 133.
- 5d) Problem 4, page 153.
- (see http//wordsmith.org/anagram)
- 5e) Problem 5, page 153.
- 5f) Problem 11, page 155.
- 5g) Regular Expression
25In-Class Exercise 5b Print a Pay Check
- Write a script that asks for an employee name,
and then requests the hours and rate of pay using
the input name as part of the prompt - Name of Employee? _Monty Python_
- Rate of pay for Monty Python? ___
- Hours worked by Monty Python? ___
- Print out a "check" with a bank name of your
choice, today's date using the date function,
"Pay to the order of " in front of the person's
name, and the pay following a dollar sign using
0.2f format (rate times hoursdon't worry about
taxes). - disp('Union National Bank '
date)fprintf('\nPay to the order of s the
amount of 0.2f\n', name, pay)
26END