Professional Documents
Culture Documents
Cookies and Cream
Cookies and Cream
ALGORITHMS &
COMPUTING I
Asst. Prof. Axel POSCHMANN
AY 2012/13 Semester 1
4/9/12
Lecture 4: Scripts
L4-2
Algorithms
MATLAB scripts
Documentation
Input
Output
Printing Vectors and Matrices
Scripts with Input and Output
Lessons Learned
4/9/12
Lecture 4: Scripts
L4-3
Algorithms
Definition:
An Algorithms is the sequence of steps needed to
solve a problem.
4/9/12
Lecture 4: Scripts
L4-4
Top-Down Approach
Top-Down Design approach:
Break down the problem solution into separate steps
Refine each step until they are small enough to be manageable
tasks
Example: calculate the area of a circle:
1. What information is required?
radius
2. Given the radius, calculate the area
area = 2*pi*radius
3. Display the output
4/9/12
Lecture 4: Scripts
L4-5
4/9/12
Lecture 4: Scripts
Further Refinement
1. Where does the input come from?
External file
One of them is the default input device
User
Input is now two steps:
1. Prompt the user to enter data
2. Read data into program
L4-6
4/9/12
Lecture 4: Scripts
L4-7
4/9/12
Lecture 4: Scripts
L4-8
MATLAB Scripts
Definition: Interpreter
Goes through the code line-by-line
Executes commands as it goes
MATLAB interprets script files (also called M-files)
A program is a set of scripts and functions.
A script is a sequence of MATLAB instructions that is
stored in an M-file.
4/9/12
L4-9
Lecture 4: Scripts
M-files
Used to store scripts
Can be displayed by the type command
Can be edited by the edit command
Can be executed by entering its name at the prompt
to display
to edit
to run
4/9/12
Lecture 4: Scripts
Creating Scripts
2 simple steps to create a script:
Check that the current folder is correct
Either Click File -> New -> Script or
Press ctrl+N or
Type edit
L4-10
4/9/12
Lecture 4: Scripts
L4-11
Documentation
It is very important to document scripts well, so that
4/9/12
L4-12
Lecture 4: Scripts
Input
Input statements read in values from the standard input
Prompt
this is what the user
will see on the screen
4/9/12
Lecture 4: Scripts
L4-13
Input
If you want to read in a character or a string you have two
options:
either add s to the input function
>> letter =input(Enter a character: ,s)
Or add quotation marks around your input
>> name=input(Enter your name: )
Enter your name: Anonymous
name =
Anonymous
Better!
4/9/12
Lecture 4: Scripts
L4-14
Input (ctd)
Note: it is better to use option 1) because the user may
4/9/12
Lecture 4: Scripts
L4-15
Input (ctd)
If MATLAB expects a number, but a letter is input it throws
4/9/12
Lecture 4: Scripts
L4-16
Input (ctd)
Separate input statements are necessary if more than
4/9/12
Lecture 4: Scripts
L4-17
Output
Output statements display strings and/or the results of
4/9/12
L4-18
Lecture 4: Scripts
Output (ctd)
fprintpf
Allows to print formatted output on the screen
Uses a format string to format the output
Format information,
place holder
newline
character
4/9/12
Lecture 4: Scripts
L4-19
Output (ctd)
The character in the place holder is called the conversion
4/9/12
L4-20
Lecture 4: Scripts
Output (ctd)
A field width can be included in the place holder
At least 3 places
are reserved for
this expression
4/9/12
Lecture 4: Scripts
Output (ctd)
Other formatting options:
Left-justification by inserting a minus sign, e.g. %-5d
String truncation by specifying decimal places, e.g. %.2s
Special characters
Slash \
\\
Tab
\t
Quote
L4-21
4/9/12
Lecture 4: Scripts
L4-22
4/9/12
Lecture 4: Scripts
L4-23
messy end!)
>> fprintf(%d,vec)
2345>>
Using a script it is possible to separate numbers with
4/9/12
Lecture 4: Scripts
L4-24
4/9/12
Lecture 4: Scripts
L4-29
Lessons learned
Common Pitfalls:
Spelling a variable name different ways in different places
in a script or function.
Forgetting to add the second s argument to the input
function when character input is desired.
Not using the correct conversion character when printing
Confusing fprintf and disp. Remember that only
fprintf can format.
4/9/12
Lecture 4: Scripts
L4-30
Lessons learned
Programming Style Guidelines:
Especially for longer scripts and functions, start by writing
an algorithm
Use comments to document scripts and functions, as
follows:
Block of contiguous comments at the top to describe a script
Block of contiguous comments under the function header for
functions
Comments throughout any M-file (script or function) to describe
each section
4/9/12
Lecture 4: Scripts
L4-31
Lessons learned
Programming Style Guidelines:
Make sure that the H1 command line contains useful
information
Make all output easy to read and informative
Put a newline character at the end of every string printed
by fprintf so that the next output or the prompt
appears on the line below