Workshop 1: Introduction to Matlab

originally presented Oct 25, 2013

This workshop covers the basics of Matlab: setting up and starting Matlab, using the Command and Editor windows, defining variables and entering data, conduct standard statistical analyses, and basic plotting functions.

What is Matlab?

A programming language (like C, C++, Python) combined with a huge package of statistical and graphical tools. It is used by researchers across many fields to construct stimuli, run experiments, and analyze data.

Goals of this workshop:

  • Learn about the range of tools available in Matlab
  • Learn how to start Matlab and use the command window
  • Learn how to define and manipulate variables
  • Learn how to write simple scripts

Obtaining Matlab

Matlab is a proprietary software which runs on licenses (personal, group, network, or site licenses for whole campuses). Unfortunately UCSC does not (yet) have a site license for Matlab, but you can purchase your own licenses here:

The Command Window: a glorified calculator

  • List of basic Matlab commands and built-in functions
  • Defining variables
    • v(2,1)
      v = [2 5 3; 1 6 4];
      w = v.^2 + 3;
  • Plotting: scatter plots and bar graphs
    • x = [0 : .1 : 10];
      y = sin(x);
      figure(1); plot(x,y)
  • Simple stats: mean, median, std
    • [mean(x) median(x) mode(x) std(x)]

The Editor Window: writing and running scripts

  • Script = list of commands
  • Suppressing output using “;”
    • y = pi;
  • Querying variables
    • y
  • “if” statements
    • if y>3
  • “for” loops
    • for i=1:10
  • Commenting
    • %anything following a percent sign will be ignored
  • Running and debugging scripts
    • using the 'keyboard' command within scripts to pause and query variables

Getting help within Matlab (press F1 or click on Product Help) and outside of Matlab:

Matlab exercises

hw1.m (need link to file)
hw1_answers.m (need link to file)