CS112: Lab 2 :: More MATLAB

Collections of numbers, vector operations and 2D plots

MATLAB Tips of the day

Tip #1: colon notation
The 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. For example,
code broken across lines with ellipsis
Problem: You cannot use ellipsis within a string, for example:
disp broken across lines with ellipsis
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):

separate disp statements

Tip #3: The input MATLAB command
Look it up in the online MATLAB documentation. Experiment with it in the command window.

Variables and Plotting

  1. variable review worksheet
  2. simple plotting worksheet
    (Search the on-line MATLAB help with keyword "LineSpec", for more information on Line Specifications.)
  3. Assignment 1 contains an exercise called the Golden Ratio.
  4. (optional) more plotting practice: Facebook Data
  5. 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 Quit Debugging button on the top right.

Debug 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 Breakpoints 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 cs112/ folder.
The final version of your work to be turned in and graded should go into your cs112/drop/assign1 folder.