LearningMatlab42013-03-07 21:23:05localhostconverted to 1.6 markup32006-09-01 09:48:25cmbg-cache-2.server.ntli.net22006-07-27 19:52:59cmbg-cache-2.server.ntli.net12006-07-04 15:01:19Scripting Subsystemmatlab for SPMTo attain the glittering heights of becoming an SPM power user, you will need to learn some matlab programming. SPM is written in matlab, which is a high level programming language based on matrices. One of the the virtues of SPM is that the authors give out the source code, which is mainly in the matlab programming language. This means that, if you can program in matlab, you will be able to read the code to see what SPM is doing (it is quite often the only way to work this out). You can also change the behaviour of SPM by changing the code, or write code that works with SPM to do analyses that SPM does not do, or pull data out of SPM to analyse with other packages. Versions of matlab and SPMThere are two basic versions of SPM in current use: SPM2 and SPM5. SPM2 will run on matlab 5.3 or greater. I have used it with matlab 7 at least. SPM5 is recorded as needing matlab 6.5 to 7.1 Starting matlabThis may depend on your local system. On the MRC-CBU system, we start matlab in Unix, by typing, simply, at the unix prompt. This brings up the matlab window, which is a text input window, with a prompt that looks like this: >]]>To run matlab programs, you can type the names of matlab scripts or functions at this prompt. For example, to get the SPM2 file selection dialog box, you could type: at the matlab prompt. The equivalent for SPM5 is Matlab very basicsWith the matlab window, you have access to a very powerful mathematical system. At its most basic, you can use it as a calculator. For example, if you type: at the >> prompt, and press return, you will get the following output in the matlab window: Like other programming languages, matlab uses variables. For example, to set the variable A to equal 36: which will give us the output: if we then set B thus: then, as you would expect, you can type: and get the output: That was very basic, wasn't it? So, now would be a good time to have a look at some matlab tutorials. Below are some links to matlab information on the web. There is a good tutorial accessible via the Getting started link in the matlab HTML help system. You can get the HTML help system by typing "helpdesk" at the matlab prompt. You can get another version of the same tutorial by typing "tour" at the matlab prompt. See below for other help options within matlab. Matlab tutorialsThere are a large number of matlab tutorials on the web. A good place to start is the Indiana University Matlab page, which contains a basic tutorial and lots of links to other tutorials. Below is an unsorted list of other matlab links that are mainly basic tutorials: tutorial from the maths department at Utah Mark Gockenbach's matlab tutorial and the same tutorial in postscript. University of New Hampshire's tutorial MIT FAQ about matlab, with quite a number of answers that are specific to their setup. Maths and matlab links, including tutorials. Basic section of the University of Michigan matlab control tutorial. Links to some more tutorials etc. Getting help from within matlabThe following commands may be useful to get help within matlab. To use them, type the commands at the matlab prompt. lists the topics for which help is available. followed by a matlab function name, such as, for example, spm_get - the SPM2 file selection function, prints out the help available for that function in the matlab window. starts an introduction to matlab. starts the matlab HTML help system. starts an introduction to programming in matlab. followed by some word of interest, searches all the directories matlab knows about for functions with the word in the first line of help for that function. Try: for more detail. There are a couple of very useful additions to the help documentation. Firstly, the function, that can also be reached by the command As I mentioned above, there is a useful tutorial attached to the "getting started" link on the start page of this help system. There is also another help system, rather simpler in design than the HTML help, reached with the command Printed manualsDon't forget these. I found the introductory user guide for 4.2 helpful, all that time ago. I haven't looked at the equivalent manuals for later versions of matlab. Using matlab with SPMSo, you now have a basic grounding in matlab. Using matlab with SPM is broadly pretty simple, but, as with SPM generally, it is very important to understand the underlying concepts. To get started, the first thing to do is to understand the image format used by SPM. This is a modified version of Analyze 7 format; you can find details in our Analyze format web page. Next you could try my SPM statistics tutorial, and in particular the associated matlab code, . This code takes you through getting data out of image files, doing some basic adjustments to the data, plotting it, saving it to file etc. It might be useful to work through this code with the tutorial to get a feel for programming with matlab and SPM. If you want to write programs using the SPM interface, have a look at the spm_get routine (SPM2) or the spm_select routine (SPM5), in the SPM distribution, and the spm_input routine. Basic help is available via: The code itself contains more comprehensive help; try looking at spm_get.m / spm_select.m and spm_input.m in your favourite text editor. MatthewBrett