Introduction to Matlab Dr. Milica Markovic
Finding Matlab Find matlab by going to: Start All Programs Matlab
Command Window Don’t use command window to directly type-in Matlab commands, unless you want to use Matlab as a basic calculator.
Matlab as a calculator
To write Matlab program, open a new script (editor) file first by going to File, New, Script This is a script (or editor) window! Type your Matlab code here! In Matlab you don’t have to declare variables first! I just wrote a=11 Use letters, numbers and _ (underscore) for variable names. Always start the variable name with a letter.
To run Matlab program, press F5 or the green triangle shown below. Always start the name of your file with a letter, and you can also use numbers or underscore _
Clear all command clears all variables in memory Clear all command clears all variables in memory. clc clears command window contents. Always start your code with the first two lines Clear all and clc.
More tips for naming Matlab files To save the new file as yourname.m go to File, Save As. Type yourname.m Be careful when you name a matlab file: (a) Avoid all special characters. (b) Do not use space (c) Do not start the le name with a number. (d) When you start writing your code, do not name any variables the same as the name of your file. (e) Do not name it sin, cos, or any other function that is likely to already exist.
Good code-writing practice: comments!
After running code with lot’s of computations:
To suppress printing in command window Put ; at the end of the line you want to suppress
Use Matlab to help you assemble the report When you are done with one matlab code, save the matlab file, then “publish” it, by doing the following: (a) Open the File pulldown menu (b) Select Publish conguration for new_file.m (this is your matlab le name) (c) Select Edit Publish Congurations for XYZ.m (d) A new window opens whose title is “Edit Congurations"
Selecting the type of file for publising Select doc (you should also see html, xml, latex, doc. ppt and pdf)
Now Publish the File (a) Go to File, Publish (b) Be patient it takes about 20 seconds to make a published le (c) You can then edit this le, add comments and put all doc files together (d) Finally, make a pdf from your final .doc file
Basic computation
Creating Arrays Tip: To make a column vector, separate entries with a ; or use a transpose operation g=[1 2 3]’
Colon Operator start_value: step: stop_value For example: 1:0.1:2 >> clear all >> fall2012 ans = Columns 1 through 6 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 Columns 7 through 11 1.6000 1.7000 1.8000 1.9000 2.0000 >>
Another way to make an array Linspace(start, end, how_many_points) clear all clc linspace(1,100,10) ans = 1 12 23 34 45 56 67 78 89 100 >>
Creating Matrices Tip: g(2)=4, g(4)=5, g(5)=3, g(10)->error
Ploting
Add features to graph
Sinusoidal Functions
Making Cool Graphics
This is called “Graphics Window”