Agricultural and Applied Economics 637

Introduction to MATLAB

Workshop #1

I believe the best way to really learn econometrics is to go inside the black box. As such in ournext two lab sections we will develop “native” computer code that is used to estimate the parameters of the classical regression model (CRM) and calculate associated regression-based statistics. We have found that the MATLAB software package is a very useful tool for obtaining an understanding of applied econometrics as it allows you to directly translate what is presented in class into an operational software program. This software has been used extensively by econometricians. This is the third year that I will be using this software having used the GUASS software systemsince the early 1980’s. I (and you) will be using this software exclusively in AAE637 for those of you taking the class.

The best way to start learning MATLAB is to use it. This workshop will take you through a basic introduction of the software and familiarize you with the programming language. You’ll want to save your code when you are finished, as we’ll build on it over the next few days. Be aware: the steps outlined here are just one way of doing things. There are many ways to edit, run, code, etc., and coding is a very personal thing. In AAE637 you will be exposed to a variety of methods to undertake estimation and you will invariably develop your own programming style. Through the years, I have found a number of strategies that help with the development and debugging of my own computer code. I will offer these insights to you over the course of your taking AAE637. I hope you find them useful.

The MATLAB software is a very extensive system that can be used for a variety of numerical/statistical operations. You will not use all of the features and commands associated with this program during this course. You willbe able to undertake a variety of basic as well as fairly advanced tasks by the end of our MATLAB workshops. I would recommend that you become familiar with MATLAB via the use of the myriad of web-based tutorials. In the MATLAB resources section of the class website contains a few of these resources. I also encourage you to learn to use the on-line help system available from the main menu once MATLAB is running. A variety of PDF files covering a various topics is contained in the MATLAB resources section of the course website. Be warned: Do not print this material out without looking at the document(s) as some of the files are more than 500 pages long.

For those of you wanting to work at home will need to purchase the student version of MATLAB available from DOIT ( for $93. Versions for Window$ and Macs are available. This is a great deal as you are purchasing a permanent license so long as you are registered student at any college or university. It is a fully functional version of the MATLAB software system. This software is also available in various computer labs across campus including the AAE lab and the Department’s Remote server.[1]

The goal for this workshop is to make sure everyone knows how to get started developing your own MATLAB code, accessing MATLAB data sets and outputting the results in a readable format. So let’s get started.

1. LOG INTO THE AAE NETWORK

If you have an AAE account, log in as usual. If you do not have an AAE account you can login using a guest account using the username and password displayed throughout the HECC lab. (Please note: guest users are not allowed to access the REMOTE server.)

2. OPEN MATLAB

On the remote/lab computers go to the Start menu → All Programs → Statistics → MATLAB (Note: If you trying this on your own machine, you will probably have an icon on your Desktop. You will also probably locate the MATLAB program in a different location than the above.)

When you start MATLAB, the desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB.

The following illustration shows the default desktop. You can customize the arrangement of tools and documents to suit your needs. For more information about the desktop tools, refer to theDesktop Tools and Development Environmentsection of the help system.

The Command Window is open. The cursor is blinking at the MATLAB prompt. Here you can enter commands line-by-line and see your output in the same window. With the command window highlighted you can clear the Command Window by selecting editClear Command Windows <hit the return key> (This clears the screen of any characters. You will see a warning message asking if this is really what you want to do.)

You can type in the following command in the Command Window:

y=16+15 <then hit the return key> (This adds 16 and 15 and shows the value of y)

Note that with a trailing semicolon no output is shown.

3. WRITING AND RUNNING A MATLABPROGRAM.

As implied the above, you can undertake various tasks using MATLAB one command at a time (i.e., interactive mode) or by running a collection of commands at once contained within a single command file (i.e.; batch mode). To run a file in batch mode you first create an ASCII(or text) file containing these commands. This is often referred to as a script file. This file can be created using your own favorite ASCII file editor (e.g., Notepad++, Tedit, etc) or you can use the native MATLAB program editor. Using the native MATLAB editor is very useful as it checks your syntax as you create the command file. MATLAB expects all script files containing MATLAB commands end with an “m” extension. I highly recommend that you follow this convention.

Let’s assume that you will be using the MATLAB editor. Once MATLAB is started click on the 2ndbutton on the 2ndtool bar (or File→newon the main menu). This will open up an editor window where you can write an entire program and run it all at once.

The first example we will use is contained in the file example_1.m .

On the top menu click on the far right of the2nd menu bar, click on the icon to change the working directory to be where you store your program. (This window stores a history of recently accessed drives which you can pull up by hitting the down button,,located in the directory/file window.)

Once you locate the directory where example_1.m is stored, click on the MATLAB editor window to make it active and then click on File→Open to open this file. You should see something like the following: (Note, the underlined items are native MATLAB commands):

FYI: a few general comments about the code.

  • There need not be a semi-colon to end every command. If you have a semi-colon the result of the command will not be shown.
  • If a statement does not fit on one line, use an ellipsis (three periods), ...,followed by the Enter key to indicate that the statement continues on the next line. A good practice to adopt is that if yourcommand line is more than 80 characters, continue with your code on a 2nd line, indent, and keep typing.
  • Any variable to the left of an equals sign is a name I made up, e.g. that I want to define for MATLAB
  • Anything to the right of an equals sign is either a variable name I already defined by a previous command or a MATLAB command/operation/function/reserved word
  • Comments, that don’t impact the operation are located after the “%”. You should adopt a policy of using comments extensively in your code.

The MATLAB editor has the normal Windows-based file management system so you can edit and save this file like any other program you have used before if you are a Window$ user (I assume the same is true for Mac users). I would recommend that you save this file to your private network directory or your own removable storage device. Go to File→Save As, and save it in the desired location.

With your cursor located in the MATLAB editor window,run this fileby clicking on the following button located in the editor toolbar, or simply type the name of the file in the command window and hit Return.What happened? Is the matrix c what you expected it to be?

Now in your edit window, add the following to the end of your program. What do you expect it to do?

d=a*b

Run the program again and notice what happens- it doesn’t work. That is, you receive the error message in the bottom window that looks similar to the following:

If you run the program in batch mode you get the following:

The number in parenthesis provides the line number that generated the error. Although I won’t go over it here, you may want to learn how to use the program debugger. I will have Chang review this material in a later lab.

This new command doesn’t make sense since the “a” and “b” matrices are not dimension compatible with respect to matrix multiplication. Given the severity of the error MATLABdid not run the remaining portion of the program after this error. There is a useful message in the command window, though. When you get this message, it is often helpful to verify the dimensions of your matrices. You will find the Workspace window very useful for the debugging of your code. For small arrays, it shows its elements. For larger arrays, it will show its dimension. By default this window is the box in the upper right of your screen but the location can be changed or window minimizes depending on your needs. If you click on an array displayed in the Workspace window the elements of this array is displayed.

The problem with the above MATLAB command “*” performs matrix multiplication, and here you are telling it to multiply a (2x3) matrix times a (2x3) matrix (which you should remember is not possible).

Change the last line to read

d=a.*b

Also add the line

e=a*bʹ % Note the transpose

Run the code again. Does it work? What is different about these? What do these commands do?

4. READING IN A DATA SET

There are a number of methods for reading in datasets into your MATLAB software. You could load native MATLAB datasets (and similar to STATA and SAS datasets) are binary files that cannot be opened up and examined using an ASCII (text) editor. For an overview of how to open a variety of file types in MATLAB refer to the User’s Guide and the Data Import and Export section. For these workshops we will load Excel files containing the data. The first row of these files contains the variable names and the remaining rows are the data. DBMSCOPY is a useful program that can transform any ASCII or binary file of a particular type (e.g., SAS, STATA, EXCEL, GAUSS) into any other file type. Besides being able to transform data from one form to another, this software also has the nice characteristic of allowing you to view any type of data, undertake simple data management activities and to obtain descriptive statistics of this data. The DBMSCOPY manual is available on-line. Feel free to use your own data conversion software if you prefer.

In the zip file I previously emailed you contained our first data set. The data set is called china_00.xls. Unzip this file to your network drive, hard disk or USB storage device.

4.1 Accessing MATLABRemotely

You can access MATLAB remotely so long as you have internet access and a valid AAE account(If you have a MAC computer you can also access the remote server but I do not know how to do this. Talk to the HECC director for more information.) You can use the Remote Desktop feature of your operating system to connect to the AAE Remote Server. If you are at a remote location, click on START, then PROGRAMS then ACCESSORIES then COMMUNICATIONS then REMOTE DESKTOP CONNECTION. The computer you want to access is our remote servers which have the computer name (address) of: remote.aae.wisc.edu . Once you enter this address, you will then see what looks like another Windows session. What is actually occurring is that your remote machine is in fact starting a session on another Windows machine (i.e., the REMOTEserver). You can then log-in to the remote computer using your AAE username and password. The remote machine is set up exactly (or very similar to) the regular AAE lab machines. This means that you have access to all the programs in the AAE lab from your remote location. Since you are logging in using your AAE account, you have the same drive mappings as you have when you log into a lab machine. This means that so long as your data, programs etc are stored on a network resource, you have access to it from your remote location. In addition if you set the remote access options correctly on your home machine you can access not only the network drives but also your local drives.

4.2 MATLAB Code for Reading in a Dataset

Now, I would like you to use the 2ndMATLAB command fileto illustrate the use of MATLAB, example_2.m. In the MATLAB editor use File→Close to close the previously usedMATLAB command file (Note: You can have multiple files open at once if desired). Then use File→Open to open up the file example_2.m The following is a listing of the commands contained in this file (MATLAB functions in bold).

clear; % Command to clear memory

clc; % Command to clear the command window

[full_data,varnames,raw] = xlsread('china_00');

%{

Load all data from an excel dataset

variables: fafh, region, fah, percinc

%}

fafh = full_data(:,1);

region = full_data(:,2);

fah = full_data(:,3);

percinc = full_data(:,4);

[numobs,numvars]=size(full_data); % Determine the number of rows

delete('w:\example_1.out') % Deletes a previously created output file

diary('w:\example_1.out'); % Open up a txt file for output

diary off;% Turn off the sending of output to the output file

var_names=char('fafh','region','fah','percinc');% var_names is a (4 x 1) character array

disp'I have successfully run the program' % Show this text in the command window

This block of text, or something very similar to it, will be needed at the beginning of virtually all ofyour MATLAB programs.

What do the above commands do?

clear: A MATLAB command clears the workspace from previous batch runs or command line inputs (

clc: A MATLAB command that clears the command window (

xlsread: Command to read in a Microsoft Excel spreadsheet file. There are a number of different structures of this command. Below is a listing of these alternative structures obtained from the xlsreadhelp entry:

[num,txt,raw] = xlsread(filename)

[num,txt,raw] = xlsread(filename,sheet)

[num,txt,raw] = xlsread(filename,range)

[num,txt,raw] = xlsread(filename,sheet,range)

[num,txt,raw] = xlsread(filename,-1)

[num,txt,raw] = xlsread(filename,sheet,'','basic')

[num,txt,raw,custom] = xlsread(filename,sheet,range,'',functionHandle)

The command [num,txt,raw] = xlsread(filename) reads data from the first worksheet in the Microsoft Excel spreadsheet file named filename and returns the numeric data in array num(this can be any name I want). With this command structure, it optionally returns the variable names in the cell array txt(this can be any name I want), and the unprocessed data (numbers and text) in cell array raw (this can be any name I want). (

fafh: Created variable containing the (numobs x 1) vector of food away from home expenditures

region: Created variable containing the (numobs x 1) vector of region of residence

fah: Created variable containing the (numobs x 1) vector of food at home expenditures

percinc: Created variable containing the (numobs x 1) vector of per capita income

size: Returns the dimension of the target matrix in separate variables numobs(rows) and numvars (columns). (

numobs: A created variable (this can be any name I want) that contains the number of observations in the dataset

numvars: A created variable (this can be any name I want) that contains the number of variables in the data matrix

delete: This command is used to delete the named file. If the file does not exist you will receive an error message. (

diary: Is usedto write a copy of all subsequent keyboard input and the resulting output (except it does not include graphics) to the named file, where the filename is the full pathname or filename is in the current MATLAB folder. If the file already exists, output is appended at the end of the file.(

diary off: This command suspends the diary command. (Note that diary on resumes diary mode using the current filename, or the default filename diary if none has yet been specified.)

5. RUN THE CODE, AND CHECK TO MAKE SURE YOU HAVE

SUCCESSFULLY LOADED THE DATA

With the above program in the editor, click on the run active file button, on the main editor menu. In the Workspace area you should see the four vectors of data plus the underlying matrix each with 2050 observations. You should see the following displayed in the Command window: “I have successfully run the program”.