Effective Programming Tips for Cogent Session 10, 27.3.2008 Christian Kaul MATLAB for Cognitive Neuroscience.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
How to Think through your program Session 5, Christian Kaul MATLAB for Cognitive Neuroscience.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
MATLAB Programming Session
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
Topics Introduction Hardware and Software How Computers Store Data
1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Program Design CMSC 201. Motivation We’ve talked a lot about certain ‘good habits’ we’d like you guys to get in while writing code. There are two main.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Control of (some) External Devices using MatLab/Cogent Steffan Kennett,
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Understanding the TigerSHARC ALU pipeline Determining the speed of one stage of IIR filter – Part 2 Understanding the pipeline.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Code Conventions Tonga Institute of Higher Education.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Experimental control in COGENT/MATLAB Christian Ruff.
1 Software. 2 What is software ► Software is the term that we use for all the programs and data on a computer system. ► Two types of software ► Program.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
CRE Programming Club - Class 2 Robert Eckstein and Robert Heard.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Getting Started With Python Brendan Routledge
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Creating a script create new blank document. Editor options Docking and undocking tabs.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Component 1.6.
MATLAB – More Script Files
Topics Introduction Hardware and Software How Computers Store Data
Welcome to Arduino A Microcontroller.
CS190/295 Programming in Python for Life Sciences: Lecture 1
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Topics Introduction Hardware and Software How Computers Store Data
Functions In Matlab.
Unit 6 Assignment 2 Chris Boardley.
Functions Christopher Harrison | Content Developer
Python 19 Mr. Husch.
Stata Basic Course Lab 2.
How to write good code Part 2
Matlab Basics Tutorial
Python 19 Mr. Husch.
Software Development Techniques
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

Effective Programming Tips for Cogent Session 10, 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

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.

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. Effective programming – the most important

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'];% 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']);

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

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 me or Christian Ruff. function set_parallel_port(action,pattern) % pattern is an 8-bit binary string, e.g. ' ' 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;

Thank you…