CS112: Lab 2 :: More MATLAB
numbers, vector operations and 2D plots
MATLAB Tips of the day
Tip #1: colon notation
colon notation is very helpful when generating vectors: start:step:end
(step is optional)
10:20 ==> [10 11 12 13 14 15 16 17 18 19 20]
10:5:20 ==> [10 15 20]
10:6:20 ==> [10 16]
0:0.2:1 ==> [0 0.2 0.4 0.6 0.8 1.0]
Note that 0:0.2:1
is equivalent to
colon(0, 0.2, 1)
Tip #2: ellipsis
If you happen to find yourself typing in a looooong MATLAB expression,
you can use an ellipsis (...) to indicate line continuation.
Problem: You cannot use ellipsis within a string, for example:
MATLAB often uses color to indicate how it understands your code.
See the red that turns into black and then purple above? That suggests a problem.
Solution? Break your statement up into separate disp commands, like this
(note that the code below is part of an m-file, rather than code
typed directly into the command window):
Tip #3: The input MATLAB command
Look it up in the online MATLAB documentation. Experiment with it in the command window.
- How can you get a number back?
- How about a string?
- What if the user hits Return without having entered anything at all?
- What happens if you add or omit the semicolon (;) at the end of the command?
Variables and Plotting
- variable review worksheet
- simple plotting worksheet
(Search the on-line MATLAB help with keyword "LineSpec", for more information on Line Specifications.)
- Assignment 1 contains an exercise called the Golden Ratio.
- (optional) more plotting practice: Facebook Data
- Now you're ready for the rest of Assignment 1
** Important Note about MATLAB's debug mode **
Sometimes, when there is an error in your program, MATLAB decides to go into
Debug mode. You will know this because the MATLAB prompt in the Command Window
has a K in front of it like this:
To get out of Debug mode, go to the Editor window, and click on the red
button on the top right.
mode sometimes also puts breakpoints into your code, these will show
up as little red filled circles to the left of your line of code.
To get rid
of these breakpoints, in the Editor window again, click on the
button. Then select the
Clear All option.
(Later on in the semester, we will use Debug mode to help us understand how
our programs are working, but for now, we simply want to exit out of Debug mode
when MATLAB puts us in it).
Uploading your saved work
Use Fetch to upload your saved work.
Work-in-progress should go into your personal
The final version of your work to be turned in and graded should go into your
- When done uploading, be sure to delete your working folder from the desktop (by putting it in the trash can) and then empty the trash (Finder--> Empty Trash).
- Make sure to Exit out of MATLAB when done