Download presentation
Presentation is loading. Please wait.
Published byRosemary Melton Modified over 9 years ago
1
Effective Programming Tips for Cogent Session 10, 27.3.2008 Christian Kaul MATLAB for Cognitive Neuroscience
2
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
3
Effective programming
4
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
5
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
6
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.
7
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
8
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
9
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
10
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
11
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. Effective programming – the most important
12
handling files
13
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
14
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'];% data file name (one per trial) –datafilename=[subjectnamerun, '_AttentionChoice_behav_data.mat'];% results file name (one per scipt execution) –if exist(filename,'file') – error('Results file already exists!'); –end –For trial = 1:NoOfTrials – % script main body here –Datafilename = [datafilename int2str(trial) ‘.mat’]; –save(datafilename, 'data');% save data(!) after EACH trial –End % trial –Clear data% 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']);
15
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
16
Peripheral devices
17
Cogent has very easy-to-use functions that allow handling all kinds of peripheral devices on –Parallel ports –Serial ports –USB ports
18
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.
19
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
20
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)); % set pins wait(10) outportb(888, 0); % set pins end return;
22
Thank you…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.