Programming in Matlab: (Attaway 2, 5)
Zen and the Art of Programming
Programming is like building a motorcycle in that we want to create a
complex and beautiful artifact
functions correctly and efficiently
Have tools, have parts.
Need to select right tools, right parts, understand them well enough
to get the job done.
Or preferably better, as then we can recognize
a superior part or tool if we run across it.
So tadpole, begin by learning about the functions
and commands of your language, and the syntax they entail.
Master them, for beyond, loom great
strategic issues like hierarchical, modular design...
(Of course in reality, programs are not always beautiful...
or as functional as hoped...
Matlab Is what it Is
Matlab is forgiving, for a programming language, but it's still very
literal-minded (of necessity ... can't have ambiguity). So can't say
when you mean
Learn the syntax. That is, the "grammar" of the language.
Bad syntax slows down programming, derails your train
Bad enough syntax means error messages can be unhelpful.
Learn the semantics, That is, the "meaning" of the
instructions you write.
Being unclear on the semantics
leads to fuzzy thinking (fine in its place,
but unhelpful when encoding algorithms).
Algorithms (Attaway 2.1)
Intuitive Notion of Algorithm
- Get input.
- Process input using a well specified procedure to obtain results
- Output results
Computational Notion of Algorithm
- Get input in discretely coded (symbolic) form.
- Process input, generating discretely coded results,
using a finitely represented discrete process that is
described using a formal specification language (e.g. a Matlab program
or a Turing Machine description)
- Output results to some user.
- The concept is sometimes broadened to
include multiple, interacting input and output phases.
Input can come from many sources,
and represent many things, including simple numeric objects, text,
sound, imagery, structured data such as coded census forms,
or input from any number of sensors or detectors.
Sometimes input is stored data and can be processed at leisure.
This is sometimes referred to as batch mode.
In other cases, the input is associated with some ongoing process.
and must be processed under real-time constraints of varying
Input can involve complex real-time methods interacting with
Compact Muon Solenoid Detector (Wikipedia). Find the person for
sense of scale. Generates 40 Terabytes (40,000,000,000,000
bytes) of data per day!
For us, input from keyboard, files, microphone, analog-to-digital
maybe mouse and other user-designed GUIs (Attaway p.405).
Processing could involve millions of lines of structured code developed
over decades by hundreds of individuals, institutions, and
The result is not always pretty.
Processing is frequently interleaved in complex fashion with
input and output operations.
In modern applications, there are often several to many processing
resources available, some special purpose, others more generally targetable,
which operate simultaneously and in parallel, and must interact with each other
in precisely defined ways.
Coordinating such multiple resources is an immensely difficult
issue that is the subject of several entire subfields of
We will largely avoid the complexities of parallelism in this class,
but note that the machine on your desk contains dozens of separate
computational elements, all interacting
(reasonably) smoothly and efficiently.
Standardized interfaces are key.
Keyboard Input (Attaway 2.3)
To get user input via the keyboard, use the input()
built-in function. For instance
My_joules = input('Enter work (in joules): ');
causes matlab to print
Enter work (in joules):
to the command window, and then wait for the user to type a
numeric value followed by a return.
This value is then assigned to the variable My_joules.
Input() can be used to
read strings as well: either the user response is in quotes or
use ('s') as optional 2nd argument:
new_string = input('Enter a string', 's');
Also vectors and matrices if the [1 2 3; 4 5 6; 7 8 9] format is used.
Printed Output (Attaway 2.3)
Printed output can be sent to the command window using either
disp() or fprintf().
disp() takes a single argument,
(which can be a vector or matrix or string)
and displays its value in a format chosen by matlab. For example,
disp('Input must be positive!'); % an error message
disp(sqrt(a^2+ b^2)); % answer for a computation
fprintf() can be used to generate more complicated output
with a format chosen by the user (e.g. in printing tables).
It is very similar to commands found in C, C++, and lots of other languages.
The details can be complicated, and you can look them up using help
or other references (the book...).
The basic idea is a string with
slots indicated by "%xxx" for the values to be printed, where "xxx"
is a code indicating what sort of value is to be printed, and how it is to
fprintf( 'Y(%d) is %f \n',i, Y(i));
prints something like
Y(43) is 77.4532
The format string indicates that an integer value
(indicated by %d) and a float
value (indicated by %f) are to be printed in the
string at the specified locations, and are obtained by evaluating
i and Y(i) respectively.
Note on printing out formatted vectors and arrays:
Since most arrays are not part of the ultimate output for a problem,
printing them mainly shows up in debugging...
This can be easily done by
just mentioning their name (without a semicolon) to get
matlab to print the current value, and then commenting out the line when
no longer needed.
A = Some_Function(B, C,D);
A % comment out when not needed
%B used to print out B, now I don't
File Input (Attaway 2.6)
Getting data into a computer for processing has always been a bit
of a bottleneck. Originally punch cards were used.
We can initialize variables and arrays by typing to
the command window, and we can get user input
using the input() function, but this is slow.
Particularly a killer if you have to leave Matlab and come back to work
later - all that data you entered or generated has to be re-entered
Matlab provides a solution to the latter problem with the
load command, which permits variables and arrays previously saved
to a file (with the save command)
to be restored.
Simplest version is simply
which searches for a file called matlab.mat into which
Matlab state was (presumably) previously saved.
Saved variables are created and assigned the previously stored value.
Any existing variables with the
same name as a saved variable are overwritten.
Variations allow a particular file to be specified, so that separate
sessions can be restored.
If an ascii file is read (no .mat extension suffix)
the data in the file, which must be a rectangular array of numbers,
is read into an array variable with the file name. E.g.,
reads the data into a variable called mydata
(note the extension .dat is lost)
With care, a file with a rectangular array of numbers can be created
by a text editor, or a program in some other language, and thus
read into Matlab.
This provides a way of importing external data.
File Output (Attaway 2.6)
Review: Scripts and Functions
Write a script that calls functions....
E.g. in current programming exercises, Attaway 5.18, 5.23.
Say we want: function to get input from user, function to compute
function to plot. All held together by a script.
Given a function mpg() that returns mpg given a vehicle's weight and
write a script to get data from user, derive his mpg at a range of
and plot the result. Need four .M files and a script:
function [min_speed, max_speed, weight] =
% perhaps query user, read a file, whatever...
function mileage = mpg(speed,weight);
% given from physics or empirical model:
% returns one mpg figure for given speed and weight
function [speedvec, mpgvec] =
mpg_range(lowspeed, highspeed, wt);
% calls mpg() for a wt and range of speeds
function display_mpg_graph(speedvec, mpgvec, title_string);
% plots mpg for my range of speeds and weight
% with given title.
% main script (note use of names!)
[lo, hi, wt] = get_speedrange_and_weight;
[xvec, yvec] = mpg_range(lo, hi, wt);
display_mpg_graph(xvec, yvec, 'MY Hummer MPG');
Programming and Problem-Solving
- Programming is useful as metaphor and method for
- Clarifies the degree to which you actually understand a problem and
- Many 'Programming and Problem-solving in X' books
- Hard to find principles, easy to find syntax lessons
- Some classic books like
assume too much experience for us, but are good to aspire to.
- Matlab books are often nothing but lots of recipes using mysterious
matlab capabilities cutely. Fine unless you want to understand something.
- Attaway is best of Matlab crop, IMO
There have always been graphic aids for programmers to plan or document
algorithms. They're trendy, depending on the characteristics of the
languages and current 'wisdom' (theories).
In flowcharts, emphasis is on program flow, loops, testing. Box shape
functionality. Can get huge.
From Google Images, which has quite a lot of amusing and edifying
Unified Modeling Language
What would one expect in the days of XML?
These later diagrams emphasize the object-oriented model:
program pieces as objects to which one sends messages and from which
one gets replies. What goes on inside is generally not to be visible
A Good Bad example: Matlab 2-D arrays are actually stored as 1-D
arrays, and you are invited to use this fact (say by indexing
with a single dimension) rather than not know it.
Abstract Data Structures, Objects and Methods: Ways to keep knowledge about
structure from user. Yields transparency, modularity and allows
invisible revision of the implementation.
Just our opinions..
Programming for Debugging
- Write Debug-friendly code: write small functions, do big formulas in
small parts with intermediate results that you might understand as
they evolve and will print with a simple ; deletion.
E.g. in a nested
for-loop to compute mean of a matrix we might have written
mymean = (mymean + matrix(row, col)) ...
Trying the "simple input, drop the ;", strategy shows something's wrong but isn't
too helpful running on [1 1 1]:
mymean = 0.3333 % as expected...
mymean = 0.4444 % oops.
mymean = 0.4815 % yikes!
Even this simple formula may have too much going on for
easy debugging since all the 'necessary parts' are stuck in one
statement. We could rewrite the code to examine relevant partial
results, or we could think -- either way we might wind up
by rewriting the formula above to expose its parts:
its gist is
mean = mean/N + xi/N
Is that what we really want?
- Sometimes even save intermediate data for later examination.
- Run simple cases. E.g. to test your matrix-mean program, try it
on [1 1], then [1 1 1],...
- Run boundary cases (smallest, biggest)
- Make sure results are reasonable
- Recursively apply all these ideas to ever-higher levels of
calls (more complex programs using functions already debugged)
- NEVER use real data until you use data you understand, probably
made up yourself to be perfect, whose answer you know
Debugging is optional CSC160 content. It might grab you or leave you
cold. Enjoy or ignore.
I've only used Matlab's debugger a bit, but it's standard, easy, and worth
getting acquainted with, since such a tool can be VERY helpful in a
language like C or Java.
A Matlab Debugging Doc Page
- Tracing...following every statement's execution. Use
echo my_fun on; echo my_fun off ,
other echo variants.
- Command line: see help debug (a dozen debugging commands).
At a breakpoint, execution stops,
control returns to command line and you can single-step, examine and
- Single-stepping (over function calls and into function calls)
- Setting and clearing breakpoints
- Examining the call stack (you see this when Matlab has an error in
a nested function call). Stack shows where all the calls came from in their
- Graphical Debugger: in editor window after a breakpoint
- Examine variables: after a breakpoint, hover the mouse over a
variable in editor window.
"Busy Work !?!"
This course is only partly about programming, and less about Matlab.
It's mostly about the scientific and engineering techniques that
make up the rest of the course.
This future material is not "busy work" for random Matlab practice.
Far from it -- it is Matlab whose importance recedes (check the grade
weightings on the projects).
Matlab, as well as mathematics, are now just tools
to use (and we hope expand your command of) for experiments and analysis
in several centrally-important engineering techniques.
Last update: 04/22/2011: RN