Experimental Data Analysis Lab

PHYS 391 - Fall 2017
MATLAB Information

Updated 22 September, 2017 10:10

Overview

MATLAB is a widely used system in Science and Engineering for performing computation, data analysis, and data visualization. We will only scratch the surface of what MATLAB can do, but getting some hands-on experience with this powerful tool is one of the main goals of PHYS 391.

IMPORTANT: Each student needs to come up with a way to run MATLAB which they are comfortable with, as many of the lab assignments will involve doing some amount of work in MATLAB. Note that you are welcome to request and use an access code to Rm. 17 Willamette so as to have access to lab computers hosting MATLAB.

MATLAB Tutorials

The book Getting Started with MATLAB by Rudra Pratap is a pragmatic introduction to MATLAB which should be all you need for this course. MATLAB has a very good built-in help system (see Product Help under the Help menu), which also includes a series of tutorials (see ). These may also be useful to look at for some students. There are also a variety of online tutorials and quick reference guides available. Some are listed from the Mathworks MATLAB Tutorial page. For students interested in a more detailed reference, I would recommend the book Mastering MATLAB. Note that the behavior of MATLAB has changed slightly over the years, so some older tutorials may not work exactly the same under the current release of MATLAB, but most things should be the same.

Getting MATLAB

Each student needs to find an acceptable solution to allow them to work regularly with MATLAB. The University of Oregon has a site license for MATLAB which gives you permission to run MATLAB on UO machines, but does not give you rights to install it on your own laptop. MATLAB does run on Windows, Mac OS X, and Linux, and all user files are portable between the different versions, so you should be able to find one or several solutions which work for you.

The following is a list of possibilities in rough order of ease for the student.

Running MATLAB

If you have successfully found a version of MATLAB which you can run, you should be able to work through the tutorial exercises in Pratap. If you have a complete install with graphics, however, it is good to check that you can make a plot using the commands below.

>> theta = linspace(0, 2*pi, 100);
>> plot(sin(theta), cos(theta), 'ro-')

Note that linspace builds an array of 100 values. You could also specify a specific spacing by using theta = 0 : 0.1 : 2*pi or, equivlant to the linspace call: theta = 0 : (2*pi/99) : 2*pi.

Using MATLAB

It is very convenient to be able to type commands in at the MATLAB prompt and manipulate values interactively.

This is also a very poor habit to get into for doing any real work in MATLAB. Anything worth doing once will almost certainly end up being done again, and anything beyond a very trivial manipulation will typically involve several steps to complete. Further, using programming constructs such as FOR or IF statements is tricky when simply typing commands interactively at the MATLAB prompt.

You should very quickly get in the habit of writing M-files with all your commands and executing these M-files by simply typing the filename (without the .m suffix) at the MATLAB prompt. These 'M-files' can be scripts or functions.

Scripts: This is a series of commands in a file that gives you a direct record of what you have done, and once you get it working properly, you can save it to use in the future. If you are working in the full MATLAB environment, it is easy to create and edit MATLAB M-files through the browser window, for example you can copy and paste a series of 'known good' commands from your history window and paste them into your script.

Funtions: These look like scripts, but have a particular first line: function [outputvarA,outputvarB,...] = myfunction (inputA,inputB,....), where 'myfunction' is a descriptive name for your function that you choose (and name the M-file with). You should try to starting setting your MATLAB work in the form of functions as soon as possible.

Just make sure you know where your files are going by setting the Working Directory appropriately before you start. Depending on the version of MATLAB and the platform, this may not be what you expect. MATLAB uses a 'PATH' which is a list of all file directories ('folders') it looks in for function names you type at the prompt or call from scripts. Functions need to be on the path in order to be found. Use the PATHTOOL to set this up.

(note below, UO no longer supports Matlab running on the shell servers under our site license. If you wish to remotely access Matlab on a UO server, you'll need an account on aciss. You'll need to see the instructor to set this up.)

Installing X11

To get graphics when running MATLAB remotely from a command line , you need to have an X11 server running on your local machine. Cygwin provides one way to do this on Windows PCs. On OS X, you may need to install X11.app which isn't part of the system by default depending upon how old your system is. From 10.7 onwards (I believe) it is automatically part of the system, but earlier systems need to install this from the "Developer Tools". Once X11 is installed and running, use the Terminal.app to ssh into the remote server.

>ssh -Y shell.uoregon.edu
 (note, shell doesn't provide access to MATLAB, nor octave, nor scilib)

The -Y option causes any X11 windows opened by your shell on hpc to be tunneled through the ssh connection and appear on your local machine. Linux uses X11 natively, so you only need to make sure you use the ssh X11 tunnel. This varies for different varieties of ssh/Linux, but will likely either be -X or -Y.