Labs‎ > ‎

Lab 1.2 Variables, Functions, and Simple Plots

Introduction

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:

A = [ 4 8 6 5 7 3 0 9 12]

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.

Naming variables

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:

myvariable = 5  % fine
my_variable = 5 % fine, underscores are fine
5th_variable = 5 % does this work?
my+var = 5  % does this work? 
my var = 5  % does this work?

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? 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. Here, I've colored them green in the same way as Matlab so they are easy to distinguish from code, but the color does not affect their function.

Accessing the elements of a variable

We use parenthesis () to access the elements of an array or matrix variable:

A(1)  % the first element
A(end) % the last element
A(5) % the 5th element

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:

A(1,2)  % the 2nd column of the 1st row
A(1,end) % the last column of the 1st row
A(1,:) % the entire 1st row; the colon (:) inside parentheses means "everything"

Functions in Matlab

Matlab has a number of built-in functions that can operate on variables.  Let's try a few simple ones:

mean_a = mean(A)

median_a = median(A)

percentile_a_15 = prctile(A,15)

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.)

Definition

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:

help mean

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:

lookfor percentile 

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:

mean = 5

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):

mean

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:

mean(1)
mean(A)

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:

clear mean

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.

mean(A)

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 

cd /Users/vanhoosr/Documents/MATLAB/Lab1_1

(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.

% generates "bell curve" data with mean 1 and standard deviation 1
% multiplying the output by 100 changes the mean to 100 and
% standard deviation to 100
mydata1 = 100*generate_random_data(10,'normal',1,1); 

% generates "bell curve" data with mean 0 and standard deviation 1
mydata2 = 100*generate_random_data(10,'normal',0,1); 

Let's look at the raw data, just as in the games last time

mydata1,
mydata2,

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

mean(mydata1)
mean(mydata2)

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.

f = figure;

The function figure pops up a blank window for us to use.  We can plot one of the data sets using the plot command:

plot(1,mydata1,'og')

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.)

hold on
plot(2,mydata2,'sb');

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:

myax = axis

We can also set these values with the axis command:

axis([0 3 -300 500])

We should add some labels to our graph using the label functions:

xlabel('Class number');
ylabel('Change in student happiness');
title('Change in student happiness in Class 1 vs. Class 2');

Character strings

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:

mystring = 'This is a test'

You can examine the elements of string just like other types of array data:

mystring(1)
mystring(end)
mystring(5)



Let's create a new data set and make another plot.

% generates "bell curve" data with mean 10 and standard deviation 1
mydata3 = 100*generate_random_data(10,'normal',10,1); 
% generates "bell curve" data with mean 0 and standard deviation 1
mydata4 = 100*generate_random_data(10,'normal',0,1); 

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

f2 = figure;
plot(1,mydata3,'og')
hold on
plot(2,mydata4,'sb');
axis([0 3 -300 1500])
xlabel('Class number');
ylabel('Change in student happiness');
title('Change in student happiness in Class 1 vs. Class 2');

Now we can switch between these figures by calling the figure command with an input argument:

figure(f) % brings the 1st figure to the front; any drawing would now happen here
title(['This is the first figure']);
figure(f2); % brings the second figure to the front
title(['This is the second figure']);

You can also use the figure number.  If the first figure is "Figure 1", you can bring it to the front with 

figure(1)
title(['This is still the first figure']);

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.

Function reference

Matlab functions and operators

User functions

  • generate_random_data

Copyright 2011-2012 Stephen D. Van Hooser, all rights reserved.
Comments