Getting Started with MATLAB


MATLAB means MATRIX LABRATORY. It is extremely user friendly (after you get use to it) and flexible. MATLAB is not built for doing statistics per se, but one can program various estimators and procedures easily and quickly using MATLAB. This page should not be used as a substitute for the manuals (which are located in most owlnet labs). Another nice feature of MATLAB is that it will produce nice plots and graphs. However, this area goes beyond the scope of this page.

One Quick Note

If at anytime you get confused using MATLAB (cannot remember the name of a function, questions about a function, etc.) you can type "help function" to get help on all MATLAB functions. Typing "help" will bring up a list of all help topics. One can use this to find function names and uses. It is vary helpful.

Reading In Data

MATLAB syntax is founded on the following form:

load ascii_data_set -ascii;

This will read the entire data set into a matrix called "ascii_data_set." The -ascii flag tells MATLAB that it is loading in ascii data. This can then be manipulated using other MATLAB commands. There is no limit (that I know of) for the length of file names, parameter names, or function names in MATLAB. Type "help load" at the MATLAB prompt for more information.

Missing Data

MATLAB does not like missing values. It will refuse to read in data sets with missing variables. Therefore, if the data set that you are using contains missing variables, you should remove them using another program before attempting to read the data into MATLAB.

Data Transformation and the Creation of Variables

Data can be transformed relatively easily in the program. For example logarithms, differences, or restrictions can be carried out with no difficulty. For example, if you have an n-by-k matrix named "data", you can create a new matrix from "data" or transform some or all of the matrix.

To transform a variable into its natural logarithm, the syntax would use the variable "data" and develop a new variable "lndata":

lndata = log(data);

One word of warning: the semi-colon is not necessary but is recommended. Without the semi-colon, MATLAB will print the results of any operations to the screen. Having a large data set print to the screen is quite annoying. Use semi-colons after commands unless you want to see the results of your operations.

To transform a the 1st column into its natural logarithm, the syntax would use the variable "data" and develop a new variable "lndata":

lndata = [log(data(:,1)),data(:,2:k)];

Here, "data(:,1)" means all the rows, first column and "data(:,2:k)" means all the rows, second through kth columns. The comma between the two statements in the square brackets puts the data in columns. A semi-colon in the same spot would put the data in rows (which in this case would produce an error statement).

Estimating Models

After reading in data, one can estimate models by typing in commands at the prompt. For example,

load data -ascii;

x = data(:,2:10);

y = data(:,1);

betahat = inv(x'*x)*x'*y

This will read a data set named data. The second line makes the second through the tenth columns a matrix called x (all the rows of data are used). The third line creates a vector called y using the first column of data. The fourth line produces the OLS estimates for a regression of y on x. Other statistical procedures can be written for other estimators. Some basic statistical procedures are included in MATLAB, so use "help stats" to see if what you want is available before you start writing a program.

REMEMBER

The "help" command is your friend. You can usually get the information you need without having to go to the manuals.

 

Statistical Consulting Lab Links

 

Mission of the Statistical Consulting Lab

Recent Projects

Vet and Unix Information

Statistical Software

Data Resources

SCL Tips

Requests for Help and Discussion

  • Software Related
  • Research Related

Fall Schedule for Lunch Seminars

Statistical Links

Home to the Statistical Consulting Lab Page