You are on page 1of 3

MATLAB tutorial 1: using the command line

Start matlab: open a terminal and type matlab &


At the matlab command prompt type
a = 1
and press enter. Next
b = 1.0
and press enter. To see what ariable are c!rrently de"ined type
whos
#o! sho!ld see somethin$ li%e:
&& whos
Name Si'e (ytes )lass Attrib!tes
a 1x1 * do!ble
b 1x1 * do!ble
An array is a list o" n!mbers assi$ned to one ariable. Type
x = + 0 1 , - . / 0
we may also !se
y = 0:0./:/
Try
x1-2 = ,,
To preent matlab "rom printin$ o!t the entire array eery time we chan$e somethin$3 we add a
semicolon at the end o" the command:
x1.2 = 4105
Arrays can be manip!lated !sin$ matrix al$ebra:
' = ,6y
or
w = ,6y7'
Now try
x6x
x.6x
x6x8
x86x
Note that the sin$le dash a"ter an array means transpose 9 i.e. a swap o" the rows and col!mns. Note
also that the dot in "ront o" the m!ltiplication si$n o" the second command means we m!ltiply element4
by4element.
:et8s try to plot somethin$:
plot1x3-6x2
A new plot window sho!ld hae appeared. Now try
! = x.6x
plot1x3!2
;e can sae this plot as a pn$ "ile by
print 4dpn$
or as an encaps!lated postscript "ile by
print 4depsc,
These commands created the "iles "i$!re1.pn$ and "i$!re1.eps3 which we co!ld rename 1e.$. m
"i$!re1.pn$ plot<x4s=!ared.pn$ on the lin!x command line2 i" we wanted to %eep them.
;e will now read in some data "rom the "ile partdi.txt. :oo%s what8s inside this "ile by typin$ less
part!di.txt on the :>N?@ command line. ?se the !p and down arrows and pa$e4!p & pa$e4down to
nai$ate the "ile. ;hen yo!8re done press =.
:oad the data into matlab with:
+con$ress3 year3 total3 dems3 reps0 = textread18partydi.txt83 8As A" A" A" A"83B*38headerlines83-2
(e care"!l here3 there is a space be"ore each A si$n.
:et8s ma%e some plots:
plot1year3seats2
xlabel18#ear82
ylabel18Total Co!se Seats82
Now try
plot1year3dems.Dtotal384b82
xlabel18#ear82
ylabel18Eercenta$e82
;e can oerplot by t!rnin$ the hold on
hold on
plot1year3reps.Dtotal384r82
hold o""
;e can add a le$end with
le$end18democrats83 8rep!blicans82
#o! can moe the le$end aro!nd !sin$ the F?> by "irst clic%in$ the arrow3 then selectin$ the le$end
box and dra$$in$ it.
#o! can $et a sense "or how correlated 1or anti4correlated2 a dataset is by loo%in$ at the correlation
coe""icient:
corrcoe"1dems.Dtotal3reps.Dtotal2
$ies yo! a ,x, matrix o" coe""icients relatin$ each col!mn to itsel" 1alon$ the dia$onal2 and to the
other col!mn1s2 1o""4dia$onals2.
TASK
Now that yo! hae completed yo!r t!torial3 letGs do some scienceH
>nside yo!r home directory3 yo! sho!ld "ind a "ile called Iecent>ndices.txt. ?se :in!x to see what is
inside the "ile and what eery col!mn o" the data is. The "irst col!mn is the year and the second is the
month o" the obseration. The third col!mn is the obsered s!nspot n!mber3 the *
th
is the obsered
radio "l!x3 and the 10
th
is the obsered AE index 1a $eoma$netic index2.
To complete this tas%3 yo! m!st
12 $enerate an ima$e 1pn$2 "iles that contain the s!nspot n!mber as a "!nction o" time3 the radio "l!x as
a "!nction o" time3 and the obsered AE index as a "!nction o" time. Elot the lines in di""erent colors
and ma%e a le$end
,2 $enerate , postscript "iles3 one that contains the scatter plot o" the s!nspot n!mber on the x4axis and
radio "l!x on the y axis and one that contains the s!nspot n!mber s AE index. )alc!late the
correlation coe""icient between the two al!es and incl!de that in"ormation in the title o" the plot.
(e s!re to label all axes o" yo!r plots.
;hen yo! are done with this tas%3 email the res!lts 1- "iles2 to Jacob.heeri%h!isenK!ah.ed!

You might also like