Title: Effective Programming Tips for Cogent
1Effective Programming Tips for Cogent
- Session 10, 27.3.2008
- Christian Kaul
MATLAB for Cognitive Neuroscience
2A collection of useful things
- Effective programming
- Why does Peter only need 30 lines to achieve the
same than I do with 300 lines? - Readability Reusability
- A few exemplary pieces of code (to reuse)
- Handling files
- Peripheral devices with Cogent
3Effective programming
4Effective programming
- Why is it called a programming language?
- Matlab is a very powerful programming language
with which you can achieve nearly everything you
ever want on a PC. - important to understand its grammar using it
- Important to constantly learn new vocabulary
grammar - Important to use your language understandable
5Effective programming - Modelling
- Do you know what you want? The Modelling step!
- Modeling drawing a flowchart listing the steps
we want to achieve. Defining a model first makes
it easier to break up a task into discrete,
simple pieces. We can then focus more easily on
the smaller parts of a system and then understand
the "big picture. Hence, the reasons behind
modeling can be summed up in two words - Readability
- Reusability
6Effective programming - Modelling
- Readability brings clarity ease of
understanding. Understanding a system is the
first step in either building or enhancing it. It
makes it easy to document. - Reusability After a system has been modeled to
make it easy to understand, we tend to identify
similarities or redundancy in the smaller steps
involved.
7Effective programming the most important
- avoiding lists
- Many lists can be avoided.
- Lists clutter your code and make it hard to read.
- Therefore
- Consider the use of matrices loops whenever(!)
you see a list. - If you really need a list have it in a separate
file/function, its likely you can then reuse it
in another script.
8Effective programming the most important
- sounding variables,
- Call your variables names that tell you what they
stand for. - A script with variables i, j, a, f1, etc. becomes
very hard to understand. - Define assign your variables ONLY at the
beginning of your script. - Never hard code numbers and values inside the
main body of your script.
9Effective programming the most important
- loop structures,
- Whenever you find a line of code inside a loop
that does not contain a variable used in that
loop put it outside the loop immediately. - This way any loop or nested loops remain small
and readable. - Use CTRL-i to automatically indent your script
and your loops for better readability.
10Effective programming the most important
- control variables,
- To switch things on/off
- To switch between conditions
- control variables help you to keep your script
flexible and are a good tool to avoid hard-coding
data in your code. - Define control variables at the very beginning of
your script - Give them sounding name to increase readablity
11Effective programming the most important
- commenting,
- Comment your script WHILE you write it.
- It will take you twice the time one day after,
four times the time one month later - Group your comments neatly away to a common
indent, this way you avoid cluttering your code
and ensure readability. - Have a short explanatory commented out section at
the beginning of your script. Explain in simple
terms what you code is doing there.
12handling files
13handling files
- Everybody knows the endless boredom of copying
loads of files from one into the other directory
for many subjects, runs, etc. - Why not have it done automatically!
- Example sorting raw fMRI data
- File sort_raw_fMRI_data
14handling files
- Saving data results within your stimulus script
- outputPath 'c\home\ckaul\'
- cd(outputPath)
- subjectname input('Please enter subject name
','s') - subjectnuminput('Please enter subject run
number ') - subjectnamerunsubjectname, int2str(subjectnum)
- filenamesubjectnamerun, 'AttentionChoice_behav_r
es' data file name (one per trial) - datafilenamesubjectnamerun, '_AttentionChoice_b
ehav_data.mat' results file name (one per
scipt execution) - if exist(filename,'file')
- error('Results file already exists!')
- end
- For trial 1NoOfTrials
- script main body here
- Datafilename datafilename int2str(trial)
.mat - save(datafilename, 'data') save data(!)
after EACH trial - End trial
15handling files
- Selecting many files with SPM-select
- Many SPM functions require long long list of
filenames as input. This can be vary tedious to
program. - The SPM-select function helps!
- for run 1 runs
- define epi files in the run
- epiDir origDir '\s' int2str(subjNo)
'\' dataFolder int2str(run) - select scans and assign to
all_files - f spm_select('List', epiDir, ''
subjectss0,4 '.\.img') - add folder name to filenames
- fs cellstr(repmat(epiDir
'\',size(f,1),1) f) - clear temporary variables for next run
- f fs
- end
16Peripheral devices
17Peripheral devices
- Cogent has very easy-to-use functions that allow
handling all kinds of peripheral devices on - Parallel ports
- Serial ports
- USB ports
18Peripheral devices examples
- VETcontrol.m
- Written by Elliot Freeman to
- Control the eyetracker system in room 101.
- uses simple command strings to operate the
eyetracker.
19Peripheral devices examples
- fMRI Scanner environment, controlling the scanner
with your stimulus script - Additionally you might want to control the
eyetracker within the scanner in the same script.
- Important Cogent-functions
- getslice, Waitslice,
- get_current_slice, get_current_volume
- waitserialbite
- cgTracker
20Peripheral devices examples
- set_parallel_port.m
- Download startportb.m und outportb.m at
featherstobe - For more detailed information please email me or
Christian Ruff. - function set_parallel_port(action,pattern)
pattern is an 8-bit binary string, e.g.
'01010101'switch action case
'initialise' startportb(888)
wait(20) outportb(888, 0) case
'set' outportb(888, bin2dec(pattern))
set pins wait(10) outportb(888,
0) set pinsendreturn
21(No Transcript)
22 Thank you