Lab 1.2 Variables, Functions, and Simple Plots
In Lab 1.1, we ended by playing some statistical games. In the next 2 labs, we will begin to learn how to create the summary numbers and plots that were used in the games. Along the way we will learn some more grammar of the Matlab language. So let's fire up Matlab and get ready to go.
Working with variables and arrays of numbers in Matlab
In the last lab, we saw how to create arrays (1-dimensional matrixes, to be precise) of numbers:
Here we have created a variable named A and set it equal to an array of numbers. A variable has a name, a value, and takes up a little memory in the computer (its memory use is proportional to its length). The default variable type in Matlab represents real numbers (long doubles for you programmers), although there are other types as well.
In general, you can choose almost any name for a variable (as we'll see below), although there are a few restrictions. You can't start a variable with a number, and you can't use spaces or operators (more on operators later) as part of variable names. Let's try a few:
Comments and the % sign
What is the percent sign doing here? The % character allows one to indicate a comment, that is not executed, but is simply a note to the user. This can be either a note to one's self, which is a good idea, or a note to other people who may read one's code.
Everything to the right of the % sign is not executed, it is considered a comment.
Why are the comments in green?(on Matlab, not on our website)? Matlab automatically colors comments green in the command line so they are easier to identify. They work the same way no matter what color they are.
Accessing the elements of a variable
We use parenthesis () to access the elements of an array or matrix variable:
Since variables in Matlab are matrixes (in the case of the variable A, a 1-dimensional matrix), we can also access elements using the ROW and COLUMN form of parentheses. A has 1 row and 9 columns so we can write:
Functions in Matlab
Matlab has a number of built-in functions that can operate on variables. Let's try a few simple ones:
Here we have created 3 additional variables (mean_a, median_a, percentile_a_15) and set them equal to the mean, median, and 15th percentile of A. (Confused about percentiles? Check out Wikipedia's short topic or Kahn Academy's video on Box-and-Whisker plots.)
Functions have a name, and accept inputs called input arguments that are enclosed in parentheses and are separated by commas. Functions return outputs called output arguments; the functions we have seen up to now return only 1 output, so we have defined a single variable to be equal to the output, but in principle there could be more:
[output1, output2, ...] = function_name(input1, input2, ...)
or, when there's only 1 output argument:
output1 = function_name(input1, input2, ...)
It's important to note that the parentheses are playing a different role for functions than they do for variables: here, they are indicating the arguments to a function; when used with a variable, they indicate which elements of the variable should be accessed.
I will use bold to point out the names of functions that are being introduced for the first time.
How does one learn about a function whose name is already known?
How did I know that the function mean could accept 1 input argument and 1 output argument? I looked at the documentation using the function help:
How does one discover new functions that one might want to use?
One way is to search on Google for Matlab and the operation you want to perform: ("matlab percentile"). Matlab also allows you to search for functions using lookfor:
A word on lousy variable name choices and functions
One interesting thing about the language of Matlab is that you have the ability to give a variable the same name as a function. For example:
However, this can be quite confusing. When you type mean going forward, Matlab will assume you want to access the variable mean (that is, variables take precedence):
Now when you try to use the function, you will at best get an error, and at worst get behavior you probably didn't want, because Matlab assumes you want the variable:
Removing a variable you don't want anymore with clear
Fortunately, there is a way to remove variables that you don't want anymore, with the function clear:
Now mean once again refers to the function. If you want to remove all of your variables (don't do this now) you can just type clear by itself.
Reading data from a function (simple)
We will now call one of the functions we downloaded in Lab 1.1. Make sure you are in the directory you created last time
For me, this is
(If you need to download the data again, go to Lab 1.1)
Now we can use the function generate_random_data to create some pseudo-random data for us. We will generate "normally-distributed" data, but for now, the true statistics of the data is not important.
Q1: If the data corresponded to changes in self-reported happiness after taking either class 1 or class 2, which class would you want to take?
We can find the mean of these data with
Q2: Does knowing the mean of these data change your answer?
Figures and Plotting (simple)
Now we'd like to make some plots of this data just as the program did in the games. To do this, we will open a "figure" window.
The function figure pops up a blank window for us to use. We can plot one of the data sets using the plot command:
The plot function is one of the most commonly used in Matlab. Here, we have told it that the X-axis coordinate of our points is all the same (that is, 1) and the y values of our data should be mydata1. The string 'og' was made by carefully reading the 'help' for plot (see help plot) and tells Matlab to use 'circles' (that's what the 'o' code means) and use the color "green" (the 'g').
We can plot more than one value at the same time by altering the "hold" state of the figure. By calling hold on, subsequent calls to plot will add data to our graph. (When "hold" is off, as it is by default or when the user calls hold off, then plot clears the existing data on the axes before drawing.)
This will plot the data using square blue symbols.
Often when we plot more than 1 piece of data (and sometimes even if we just plot a single piece of data), we may want to adjust the plot range that is shown on the graph. We can do this with the function axis.
Calling axis with no input arguments returns the current settings:
We can also set these values with the axis command:
We should add some labels to our graph using the label functions:
Here we have introduced a new type of data: character string data. In Matlab, strings of characters (that is, text) are denoted using single quotes:
You can examine the elements of string just like other types of array data:
Let's create a new data set and make another plot.
Now let's make a new plot just like the one above. We want the plot to appear in a new figure, so we can type
Now we can switch between these figures by calling the figure command with an input argument:
You can also use the figure number. If the first figure is "Figure 1", you can bring it to the front with
You can put a copy of the labeled figure in your Word document but bringing each figure window to the front, clicking on its Edit menu, and choosing Copy Figure. Next, go to Word, and choose Paste.
Q3: Turn in the 2 labeled figures.
In the next lab, we will continue to focus on plotting.
Matlab functions and operators
[ ] - matrix enclosure (concatenation)
() - element selection, function arguments, or arithmetic precedence (as in math class)
Copyright 2011-2012 Stephen D. Van Hooser, all rights reserved.