Programming in MATLAB: Lecture 1: Variables, Scripts, and Operations
Programming in MATLAB: Lecture 1: Variables, Scripts, and Operations
Workspace
Command Window
Command History
Making Folders
• Click the ‘Make New Folder’ button, and change the name of the
folder. Do NOT use spaces in folder names. In the MATLAB
folder, make two new folders: IAPMatlab\day1
• help
The most important function for learning MATLAB on
your own
• To get info on how to use a function:
» help sin
Help lists related functions at the bottom and links to
the doc
• To get a nicer version of help with examples and easy-to-
read descriptions:
» doc sin
• To search for a function by specifying keywords:
» doc + Search tab
Outline
• Scripts are
collection of commands executed in sequence
written in the MATLAB editor
saved as m-files (.m extension)
Help file
Comments
Possible breakpoints
Scripts: Some Notes
• COMMENT!
Anything following a % is seen as a comment
The first contiguous comment becomes the script's help file
Comment thoroughly to avoid wasting time later
» disp('Hello World!');
» disp('I am going to learn MATLAB!');
Outline
• Variable names
first character must be a LETTER
after that, any combination of letters, numbers and _
CASE SENSITIVE! (var1 is different from Var1)
• Command window:
• Workspace:
Column Vectors
• Command window:
• Workspace:
size & length
1 2
• Element by element a
» a= [1 2;3 4]; 3 4
» d = [a;b];
» e = [d c];
» f = [[e e];[a b a]];
» str = ['Hello, I am ' 'John'];
Strings are character vectors
save/clear/load
• Use save to save variables to a file
» save myFile a b
saves variables a and b to the file myfile.mat
myfile.mat file is saved in the current directory
Default working directory is
» \MATLAB
Make sure you’re in the desired folder when saving files. Right
now, we should be in:
» MATLAB\IAPMatlab\day1
» help clock
» start=clock;
» size(start)
» help datestr
» startString=datestr(start);
» save startTime start startString
Exercise: Variables
» load startTime
» disp(['I started learning Matlab on ' ...
startString]);
Outline
• Exponentiation (^)
» 4^2
» (3+4*j)^2
t /
k 1 e
• How well will you know MATLAB at endOfClass? Name this
variable knowledgeAtEnd. (use exp)
• Using the value of knowledgeAtEnd, display the phrase:
» secPerDay=60*60*24;
» tau=1.5*secPerDay;
» endOfClass=5*secPerDay
» knowledgeAtEnd=1-exp(-endOfClass/tau);
» disp(['At the end of 6.094, I will know ' ...
num2str(knowledgeAtEnd*100) '% of Matlab'])
Transpose
• The transpose operators turns a column vector into a row
vector and vice versa
» a = [1 2 3 4+i]
» transpose(a)
» a'
» a.'
• For vectors of real numbers .' and ' give same result
Addition and Subtraction
• Addition and subtraction are element-wise; sizes must
match (unless one is a scalar):
12 3 32 11 12 3 9
1 1 2
2 11 30 32
10 13 23
14 14 2 21
0 33
33
4
111
12 3 123
.*
1 2 3 .* 2 ERROR 2
2 2
12 3 2
4 6
1
333
12 3
369
1 4 4 .*
3333 33
2 .* 2 4
3 1 3
1 2 12
22
3 1.* 3 1 3 1 3 .^2 2 2
4 3 4
Canbeanydimension
Operators: standard
4
12
12
12
111
123 3 6 9
1 2 3 * 2 11
34
^2
34
*
34
2
2
2*
123
61218
1 M
us
tbes
qua
retod
opo
wer
s
333
123
91
827
1 3* 3 1 1 1 *
3333 33
Exercise: Vector Operations
» secPerMin=60;
» secPerHour=60*secPerMin;
» secPerDay=24*secPerHour;
» secPerMonth=30.5*secPerDay;
» secPerYear=12*secPerMonth;
» secondConversion=[secPerYear secPerMonth ...
secPerDay secPerHour secPerMin 1];
» currentTime=clock;
» elapsedTime=currentTime-start;
» t=secondConversion*elapsedTime';
Exercise: Vector Operations
» currentKnowledge=1-exp(-t/tau);
» disp(['At this time, I know ' ...
num2str(currentKnowledge*100) '% of Matlab']);
Automatic Initialization
t /
k 1 e
Exercise: Vector Functions
t /
k 1 e
» tVec = linspace(0,endOfClass,10000);
» knowledgeVec=1-exp(-tVec/tau);
Vector Indexing
a 13 5 9 10
• Picking submatrices
» A = rand(5) % shorthand for 5x5 matrix
» A(1:3,1:2) % specify contiguous submatrix
» A([1 5 3], [1 4]) % specify rows and columns
Advanced Indexing 1
12 5
c
2 13
» [val,ind]=min(abs(knowledgeVec-0.5));
» halfTime=tVec(ind);
» disp(['I will know half of Matlab after ' ...
num2str(halfTime/secPerDay) ' days']);
Outline
• Example
» x=linspace(0,4*pi,10);
» y=sin(x);
» x=linspace(0,4*pi,1000);
» plot(x,sin(x));
• x and y vectors must be same size or else you’ll get an error
» plot([1 2], [1 2 3])
error!!
1 1
0.6 0.6
0.4 0.4
0.2 0.2
0 0
-0.2 -0.2
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
-1 -1
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14
Exercise: Plotting
» figure
» plot(tVec/secPerDay, knowledgeVec);
End of Lecture 1