Matlab Basics

Matlab Basics

Here's a very quick intro to some of the basic concepts and syntax of Matlab. The hope is that after covering some of these basics, and those described in the other Matlab docs pages above, you'll be able to troubleshoot SPM and Matlab errors to some degree. Experienced Matlab programmers may find most of this redundant, but there's always more you can pick up in Matlab, so please, if you experience folk've got tips and tricks to suggest, add them onto these pages!

This page looks specifically at the most basic of basics. For info on how to read m-files and m-file programming concepts, check out MatlabProgramming. As well, MatlabPaths answers questions about modifying your search paths, and MatlabDebugging touches specifically on strategies for troubleshooting - how to get more information, what parts of error messages to look for, and how you can dig through a script to find what's gone wrong.

If you take nothing else away from this page, take this: The Matlab tutorial, contained within the program's help section, is terrific. It'll teach you a lot of the basics of Matlab and a lot of things you wouldn't think to learn. The progam help is always a great place to go for more info or to learn something new. Nothing will help your troubleshooting and programming skills like curiousity - when you hit something you don't know or recognize, spend a couple minutes trying to figure out what it is, with the Matlab and SPM help. If your help within the program isn't working, it's all online at the Mathworks website, here:

As well, if you type help command into Matlab, where command is some Matlab function name, you'll almost always get a quick blurb telling you something about the function and how it's supposed to operate. It's invaluable for reference or learning on the fly.

In the following, text in fixed-width font are commands that you can type directly into Matlab.


Here are some quick basics on Matlab code and what it all means. You don't need to be expert on all of this, but it may help you understand what an error message or piece of script means - a sort of Rosetta stone, if you will.

  • Variables

    The very very first thing about Matlab to understand is what a variable is. A variable in Matlab is like a variable in algebra - it's a name given to some number. In Matlab, you can name any single number (like a = 4) or multiple numbers at once, in which case the variable is a vector or an array of numbers (like a = [1 2 3 4]). A single number is called a scalar variable in Matlab, but a single variable name can represent a huge array of numbers of arbitrary size and dimension. Creating a variable or manipulating it is done with = statements, where the left side of the = is your variable name, and the right side is the value you want it to take. If the name doesn't already exist, an = statement will create it - something like my_variable = 6005. If the name is already taken for a variable, an = statement can be used to re-assign that variable name to something else entirely, or simply manipulate the values within that variable. You can change a whole variable at once or simply a few of the numbers (or characters, or whatever) within it.

  • Workspace

    When you start Matlab, it opens a chunk of memory on the computer called the "workspace." When you create a variable, it exists in the workspace, and it stays there until you clear it, or until you re-assign that variable name to another value (which you can do freely). Only variables in the workspace can be used or manipulated. You can easily tell what variables are present in the workspace with the command whos . You can save a whole workspace to the disk with the command save filename; this will create a binary file with a .mat extension in your present working directory (more on the working directory later). That .mat file contains all the variables you've just saved, and you can clear the workspace and then load those variables again with the command load filename. You can clear the whole workspace at once with clear or clear any individual variable name with clear variable.

  • Array notation

    Sets of numbers (or characters, or other things) in Matlab are arranged in arrays - basically rectangles of rows and columns of numbers. Arrays can be only a single number - those are called scalars. They can also be single rows or columns - those are called vectors. Two-dimensional arrays (with multiple rows and columns) are pretty common, but arrays can also be three-dimensional, or even four- or more-dimensional. If you want to access a single element of the array, you use array notation, which puts parentheses after the variable name to specify which element. So if I have a 4-by-4 array called a, a(3,2) refers to the element in the third row, second column. Array notation always refers to row first, column second, other dimensions afterwards in order. You can type a(3,2) by itself in Matlab and it'll tell you what's in that spot, or you can use = to reassign that number, with a command like a(3,2) = 16. If you have a vector, you can use only one number in the parentheses - something like a(4) = 56.

    You can make arrays by using brackets. a = [1 2 3 4] will make a four-element row vector - a 1x4 matrix. Separating the elements by spaces will put them in different columns on the same row. Separating them by semicolons will put them in different rows in the same column, so a = [1;2;3;4] will make a column vector, or a 4x1 matrix. You can combine the two methods. a = [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4] will make a 4x4 matrix, with 1 2 3 4 on each row. Using the colon vector (see below) to specify a range of numbers can be a quick way to build matrices this way. There are also several commands, like eye, ones, zeros, and others, that build specify types of matrices of a specific size automatically.

  • Variable types

    There are several types of variables you can use in Matlab. These include various kinds of numbers - integer, floating point, complex - as well as alphanumeric charactetrs (arrays of characters can be treated as strings). A given standard array can only hold one 'type' of thing - an integer array can only have integer values, a character array can only have characters as values, etc. Sometimes, though, you'd like to have more than one type of information in a given array, lumping several different-sized number arrays together, or lumping some numbers and strings and so forth together into one unit. For those, you can use cell arrays and structures, two specialized forms of variables.

    • Cell arrays

      Cells array are like normal arrays, but each element of a cell array can contain anything - any number type, any other array, even more cells. Cell arrays are accessed like normal arrays, but using curly braces instead of standard parentheses, so if I have a cell array c, I might say c{3,2} = 16, followed by c{3,3} = 'cat'. I can refer to the cell itself with standard parentheses - so c(3,3) is a cell - or the contents with the curly braces - so c{3,3} is a character array above. I can refer to the cell's contents in an expression like this by following the curly braces with the standard array notation. If I have the above array, c{3,3}(2) would be a single character - 'a'. You can make cell arrays simply by using the curly braces: mycell{1,1} = 'hello' would make a single cell containing the string 'hello'. The command cell will also make empty cell arrays. There are also various utilities to convert arrays back and forth between cell arrays and standard arrays.

    • Structures

      Structures are like regular variables that are made up of other variables. So a single structure contains several 'fields,' each of which is a variable in its own right, with a name and a particular type. Fields can be of any type - numbers, letters, cells, or even other structures. SPM makes enormous use of structures in its data organization, particularly structures nested within other structures. If you have a structure called s, it might have subfields called data and name, and you could access those with a dot, like so: = 'Structure, or = [1 2 3 4]. Nested structures are accessed the same way; you could access a sub-subfield by something like = 3. You can make a structure with the struct command, where you specify fields and values, or you can create them simply by naming a new variable and using a dot - so if there was no variable called s, the command = 'Structure' would create a structure called s with a single field called name. You can add fields just by naming them in the same way, and remove them with the rmfield command. Structures can be put in arrays or vectors, so long as all the structures in the array or vector have the exact same field names and types. So you could reference s(4).data if you had 4 structures all in a vector called s. Typing the name of a structure will always tell you all its fields and what they are.

  • The colon operator

    Last among the real basic things in Matlab is the colon operator, which operates as shorthand for a whole range of numbers at once. The expression 1:10 is shorthand for "every number between one and ten, inclusive, counting by ones". You can specify a different number to count by simply by putting a different number between two colons, so the expression 1:2:10 is every number between 1 and 10, counting by 2s (1, 3, 5, 7, 9). You can use the colon operator to make values for arrays: the command a = 1:10 makes the row vector [1 2 3 4 5 6 7 8 9 10]. You can also use it in indexing other arrays, where it's particularly powerful. If a is a 4x4 array [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4], the expression a(2:4, 2:4) gives me back a new matrix which is the second through fourth rows and columns: [2 3 4; 2 3 4; 2 3 4]. I can use the colon operator by itself as shorthand for "all rows" or "all columns;" with the 4x4 a array, the expression =a(:,1:2) would give me [1 2; 1 2; 1 2; 1 2].

  • Using all notation together

    It'll take a while if you haven't programmed before, but eventually you'll learn to use all of these notations together, which make the language particularly powerful. An expression like Sess{3}.U(4).C.C(1:10, :) may look horrible on the face of it, but all it's doing is picking out a small submatrix; that submatrix is embedded in (reading right-to-left): a larger matrix (Sess{3}.U(4).C.C) inside a structure (Sess{3}.U(4).C) inside a vector of structures (Sess{3}.U) inside a cell (Sess{3}) which is part of a cell array (Sess). That submatrix would be the 1st through 10th rows of C and all of its columns. When in doubt on what an expression means, use whos and type the names of variables and fields to try and figure out what they are. Read backwards (from right to left) and try and work out the organization of the expression that way.

  • Miscellaneous stuff

    • The variable name ans is built into Matlab as shorthand for "the result of the last thing you typed in." You can assign the results of commands to other variables, like b = a(1:3, 1:3), but if you don't and just type a(1:3, 1:3), the result of that command (a 3x3 matrix) will be given the name ans. The thing to be aware of is that the next time you type a command with unassigned output - say the next thing you typed was a(2:4,2:4) - the variable name ans would be reassigned to take on the value of the latest result. Whatever was in ans before is lost! So be careful assigning your output to variable names. And if you get something in ans that you want to keep, put it in another variable right away, by saying something like myvar = ans.
    • Generally, Matlab will report the result of your command to the screen, telling you what the new value of your result variables was. If you don't want it to do that - because, for example, your result will be a 2000x2000 matrix - you can put a semicolon at the end of the line and Matlab will suppress its output. So a = ones(2000,2000); will generate a 2000x2000 matrix of all ones, but it won't print that matrix to the screen. In m-files, you'll see almost every line ends with a semicolon so that you're not barraged during a function or script's execution by results whipping by.