How to Think through your program Session 5, 4.12.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

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.
Unit 6 Assignment 2 Chris Boardley.
Chapter 2: Modularization
Study What’s that? Why? How?. School does not “do stuff” to you You do stuff to school – it is active You do stuff to school – it is active This is about.
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Flow Charts, Loop Structures
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.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Understanding the Mainline Logical Flow Through a Program (continued)
CODING Research Data Management. Research Data Management Coding When writing software or analytical code it is important that others and your future.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Topics Introduction Hardware and Software How Computers Store Data
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
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.
Top-Down Design and Modular Development
Unit 1 – Improving Productivity Instructions ~ 100 words per box.
What is Museum Box? A Museum box is a way of presenting information that allows you to create a cube project that can be shared with others. You can use.
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.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Effective Programming Tips for Cogent Session 10, Christian Kaul MATLAB for Cognitive Neuroscience.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
1 5.4 Modular Design Top-Down Design Structured Programming Advantages of Structured Programming.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
1 Project designed and created by M. Shajith Kumar.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
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.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Introduction to: Python and OpenSesame THE BASICS.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CSC 108H: Introduction to Computer Programming
Pseudocode and comments
Learning to Program D is for Digital.
Topics Introduction Hardware and Software How Computers Store Data
Writing Shell Scripts ─ part 3
Building Web Applications
Topics Introduction Hardware and Software How Computers Store Data
Unit 6 Assignment 2 Chris Boardley.
Functions Christopher Harrison | Content Developer
How to write good code Part 2
Tonga Institute of Higher Education IT 141: Information Systems
CSCI N207 Data Analysis Using Spreadsheet
Chapter 4: Repetition Structures: Looping
Tonga Institute of Higher Education IT 141: Information Systems
Pseudocode and comments
Matlab Basics Tutorial
Software Development Techniques
Hardware is… Software is…
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

How to Think through your program Session 5, Christian Kaul MATLAB for Cognitive Neuroscience

How to Think through your program “How can I spend LESS time in front of MATLAB?” … the key to effective programming…

Effective programming principles: – Language? – Modelling – Readability – Reusability – Divide & Conquer – A few exemplary pieces of code

Effective programming principles: – Language?

Effective programming - LANGUAGE? Why is it called a programming language? –Matlab is a very powerful programming language with which you can achieve nearly everything! important to understand its grammar & using it Important to constantly learn new vocabulary & grammar Important to use your language understandable … because leaning programming is just like learning a language.

Effective programming - LANGUAGE? … so you must keep learning: –Grammar: statements can be coded in different ways - learn new stuff all the time –New Vocabulary: constantly check out help & the internet for new functions –Make yourself understood: keep it easy and explain things to yourself in comments.

Effective programming principles: – Language? – Modelling

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“.

MODELLING - WEB Examples:

MODELLING - Examples: Start Stim program Type in subject number Load main task images Start presentation Produce moving dots for backgroud, 100 frames Repeat 4 times, Record response Repeat again, Calculate mean over last 5 trials Higher than alpha lower than alpha Go the next image Increase image contrast End of images? no yes Calculate overall time, save result, end

Effective programming principles: – Language? – Modelling –Readability

Effective programming - READABILITY Readability brings clarity — ease of understanding. Understanding a program is the first step in either building or enhancing it. It makes it easy to document.

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. –Consider structs to group variables & information –If you really need a list have it in a separate file/function, it’s likely you can then reuse it in another script: setupValues = getSetupValues(); Effective programming - READABILITY

sounding variables, –Call your variables names that tell you what they stand for: Instead: I = j - mean(f1), have names like: MeanBaselineCorrectedValue = result - mean(baseline) –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 - READABILITY

commenting, –Comment WHILE you write –-->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 - READABILITY

Effective programming principles: – Language? – Modelling –Readability – Reusability

Effective programming - REUSEABILITY 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 - REUSEABILITY Identify bits of your code that do stuff over and over again. Write a simple function with inputs & outputs How? Write a file ‘fnname.m’, starting with function [output1,… N] = fnname(input1,…N) Matlab will automatically recognize your function (as long as it’s on the matlab path)

control variables, –Define control variables at the beginning of your program (& comment them) –control variables help you to keep your script flexible and are a good tool to avoid hard-coding data in your code. –Give them sounding name Effective programming - REUSEABILITY

loop structures, –Keep your loops small and readable –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. –Use CTRL-i to automatically indent your script and your loops for better readability. Effective programming - REUSEABILITY

Effective programming principles: – Language? – Modelling –Readability – Reusability – Divide & Conquer

Effective programming - DIVIDE & CONQUER Divide & Conquer, Is the principle to break large tasks up into small sub-units to be able to compute / understand them. Is achieved by deciding to put simple units of your code in separate functions. Ideally, these functions can be reused

Effective programming principles: – Language? – Modelling –Readability – Reusability – Divide & Conquer – A few exemplary pieces of code

Collecting & Saving data in your stimulus script –outputPath = 'c:\home\ckaul\'; –Subjectname = input('Please enter subject name: ','s'); –Subjectnum =input('Please enter subject run number: '); –Subjectnamerun =[subjectname, int2str(subjectnum)]; –Filename =[subjectnamerun, exp_behav_res.mat'];% data file name (one per trial) –NoOfTrials = 10; –if exist([ outputPath filename],'file') – error('Results file already exists!'); –end –For trial = 1:NoOfTrials – %stim presentation here –Datafilename = [datafilename int2str(trial) ‘.mat’]; –Eval([‘save ‘ datafilename 'data‘]);% save data(!) after EACH trial –End % trial – –For trial = 1:NoOfTrials –Result(trial) = eval(['load ' datafilename int2str(trial) ‘.mat’]); – end –Final.result =Result; –Final.NoOfTrials = NoOfTrials; –… –Save([ outputPath filename],’final’); Variables at start, Sounding names Readability, Comments, Short & easy loops Different grammar, same function Using stucts to group data

Effective programming - EXAMPLES 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.m

Prewritten functions often make life much easier: Selecting many files with SPM-select –Many SPM functions require long long list of filenames as input. This can be very tedious to program. –The SPM-select function helps! – % define epi files in the run – epiDir = [origDir dataFolder]; – % select scans and assign to all_files – f = spm_select('List', epiDir, ['^' MAO5675'.*\.img$']); – % add folder name to filenames – fs = cellstr([repmat([epiDir '\'],size(f,1),1) f]);

Struggling with Errorbars? Struggling to place your errorbars at the right point? Instead of endless trial & error, search web for a better function:Barweb.m ( ange/10803) ange/10803

Thank you…