Diff for "LearningMatlab" - MRC CBU Imaging Wiki
location: Diff for "LearningMatlab"
Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2006-07-27 19:52:59
Size: 6421
Editor: cmbg-cache-2
Comment:
Revision 4 as of 2013-03-07 21:23:05
Size: 6568
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
To attain the glittering heights of becoming an ''SPM power user'', you will need to learn some matlab programming. SPM is written in [http://www.mathworks.com 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. To attain the glittering heights of becoming an ''SPM power user'', you will need to learn some matlab programming. SPM is written in [[http://www.mathworks.com|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.
Line 18: Line 18:
To run matlab programs, you can type the names of matlab scripts or functions at this prompt. For example, to get the SPM file selection dialog box, you could type: 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:
Line 22: Line 23:
Line 23: Line 25:

The equivalent for SPM5 is
{{{
spm_select
}}}
Line 58: Line 66:
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. If you have matlab 5, 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.
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.
Line 60: Line 70:
There are a large number of matlab tutorials on the web. A good place to start is: * The [http://www.indiana.edu/~statmath/math/matlab/ 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: * [http://www.math.utah.edu/lab/ms/matlab/matlab.shtml tutorial from the maths department at Utah]
 * [http://www.math.mtu.edu/~msgocken/intro/intro.html Mark Gockenbach's matlab tutorial] and the same [http://www.math.mtu.edu/~msgocken/intro/intro.ps tutorial in postscript].
 * [http://spicerack.sr.unh.edu/~mathadm/tutorial/software/matlab/index.shtml University of New Hampshire's tutorial]
 * [http://web.mit.edu/answers/matlab/index.shtml MIT FAQ about matlab], with quite a number of answers that are specific to their setup.
 * [http://cimarron.sie.arizona.edu/math.html Maths and matlab links], including tutorials.
 * [http://www.engin.umich.edu/group/ctm/basic/basic.html Basic section of the University of Michigan matlab control tutorial].
 * [http://wings.buffalo.edu/computing/sc/mat.html Links to some more tutorials] etc.

There are a large number of matlab tutorials on the web. A good place to start is the [[http://www.indiana.edu/~statmath/math/matlab/|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:

 
* [[http://www.math.utah.edu/lab/ms/matlab/matlab.shtml|tutorial from the maths department at Utah]]
 * [[http://www.math.mtu.edu/~msgocken/intro/intro.html|Mark Gockenbach's matlab tutorial]] and the same [[http://www.math.mtu.edu/~msgocken/intro/intro.ps|tutorial in postscript]].
 * [[http://spicerack.sr.unh.edu/~mathadm/tutorial/software/matlab/index.shtml|University of New Hampshire's tutorial]]
 * [[http://web.mit.edu/answers/matlab/index.shtml|MIT FAQ about matlab]], with quite a number of answers that are specific to their setup.
 * [[http://cimarron.sie.arizona.edu/math.html|Maths and matlab links]], including tutorials.
 * [[http://www.engin.umich.edu/group/ctm/basic/basic.html|Basic section of the University of Michigan matlab control tutorial]].
 * [[http://wings.buffalo.edu/computing/sc/mat.html|Links to some more tutorials]] etc.
Line 69: Line 84:
The following commands may be useful to get help within matlab 4.2 or matlab 5. To use them, type the commands at the matlab prompt.
The following commands may be useful to get help within matlab. To use them, type the commands at the matlab prompt.
Line 77: Line 93:
followed by a matlab function name, such as, for example, spm_get - the SPM file selection function, prints out the help available for that function in the matlab window. 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.
Line 97: Line 113:
for more detail.In matlab 5, there are a couple of very useful additions to the help documentation. Firstly, the
for more detail. There are a couple of very useful additions to the help documentation. Firstly, the
Line 102: Line 119:
function is much expanded, and can also be reached by the command function, that can also be reached by the command
Line 106: Line 123:
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 to the HTML help, reached with 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
Line 110: Line 127:
Line 111: Line 129:
Don't forget these. I found the introductory user guide for 4.2 helpful. I haven't looked at the equivalent manuals for matlab 5.
Don'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.
Line 113: Line 133:
So, 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 [wiki:FormatAnalyze Analyze format web page]. Next you could try my [wiki:PrinciplesStatistics SPM statistics tutorial], and in particular the associated matlab code,http://imaging.mrc-cbu.cam.ac.uk/scripts/statstalk.m. 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, in the SPM distribution, and the spm_input routine. Basic help is available via:
So, 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 [[FormatAnalyze|Analyze format web page]]. Next you could try my [[PrinciplesStatistics|SPM statistics tutorial]], and in particular the associated matlab code, http://imaging.mrc-cbu.cam.ac.uk/scripts/statstalk.m. 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:
Line 116: Line 137:
help spm_select
Line 118: Line 140:
The code itself contains more comprehensive help; try looking at spm_get.m and spm_input.m in your favourite text editor.
Line 120: Line 141:
Matthew Brett 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

matlab for SPM

To 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 SPM

There 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 matlab

This may depend on your local system. On the MRC-CBU system, we start matlab in Unix, by typing, simply,

matlab

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:

spm_get

at the matlab prompt.

The equivalent for SPM5 is

spm_select

Matlab very basics

With 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:

36*67

at the >> prompt, and press return, you will get the following output in the matlab window:

ans =

        2412

Like other programming languages, matlab uses variables. For example, to set the variable A to equal 36:

A=36

which will give us the output:

A =

    36

if we then set B thus:

B = 67

then, as you would expect, you can type:

A * B

and get the output:

ans =

        2412

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 tutorials

There 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:

Getting help from within matlab

The following commands may be useful to get help within matlab. To use them, type the commands at the matlab prompt.

help

lists the topics for which help is available.

help

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.

intro

starts an introduction to matlab.

doc

starts the matlab HTML help system.

xplang

starts an introduction to programming in matlab.

lookfor

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:

help lookfor

for more detail. There are a couple of very useful additions to the help documentation. Firstly, the

doc

function, that can also be reached by the command

helpdesk

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

helpwin

Printed manuals

Don'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 SPM

So, 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, http://imaging.mrc-cbu.cam.ac.uk/scripts/statstalk.m. 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:

help spm_get
help spm_select
help spm_input

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

CbuImaging: LearningMatlab (last edited 2013-03-07 21:23:05 by localhost)