Transcript Slide 1

Effective Programming
Tips for Cogent
Session 10, 27.3.2008
Christian Kaul
MATLAB for Cognitive Neuroscience
A 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
Effective programming
Effective 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
Effective 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
Effective 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.
Effective 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, it’s likely you can then reuse it in another
script.
Effective 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.
Effective 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.
Effective 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
Effective 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.
handling files
handling 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
handling files
• Saving data & results within your stimulus script
–
–
–
–
–
–
–
–
–
–
outputPath = 'c:\home\ckaul\';
cd(outputPath);
subjectname= input('Please enter subject name: ','s');
subjectnum=input('Please enter subject run number: ');
subjectnamerun=[subjectname, int2str(subjectnum)];
filename=[subjectnamerun, 'AttentionChoice_behav_res'];
datafilename=[subjectnamerun, '_AttentionChoice_behav_data.mat'];
if exist(filename,'file')
error('Results file already exists!');
end
–
–
–
–
–
–
For trial = 1:NoOfTrials
–
–
–
–
–
–
–
% data file name (one per trial)
% results file name (one per scipt execution)
% script main body here
Datafilename = [datafilename int2str(trial) ‘.mat’];
save(datafilename, 'data');
End % trial
Clear data
% save data(!) after EACH trial
% clear data after saving, no memory-overflow
% with all data at hand calculate behavioural results(!) at the end of your script
For trial = 1:NoOfTrials
eval(['load ' datafilename]);
end
final.result=[results.mean_p_hits results.mean_N_false_alarms results.mean_RTs];
eval(['save ' filename ' results data vectors final']);
handling 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, ['^' subjects{s0,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
Peripheral devices
Peripheral devices
• Cogent has very easy-to-use functions that allow
handling all kinds of peripheral devices on
– Parallel ports
– Serial ports
– USB ports
Peripheral devices – examples
• VETcontrol.m
• Written by Elliot Freeman to
• Control the eyetracker system in room 101.
• uses simple command strings to operate the
eyetracker.
Peripheral 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
Peripheral 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));
wait(10)
outportb(888, 0);
end
return;
% set pins
% set pins
Thank you…