Matlab Class 4 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev.

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Pages and boxes Building quick user interfaces. learning objectives o Build a quick UI with pages and boxes o understand how pages and boxes work o click.
Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)
Procedural programming in Java
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
Chapter 2: Algorithm Discovery and Design
2015 Pre-Release Practice Click the button to try a random exam-style question. Click on the reveal button to check your answer.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Area of Rectangles, Squares, Parallelograms, Triangles, and Trapezoids.
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).
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
1 Advanced MATLAB Vectors and matrices fprintf Cell arrays Structures Flow of control Vectorization Functions.
Matlab Class 7 Hristiyan Kourtev & Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of.
CSD 340 (Blum)1 Using Visual Studio CSD 340 (Blum)2 Start/Microsoft Visual Studio 2005/Microsoft Visual Studio 2005.
Procedural programming in Java Methods, parameters and return values.
Debugging Visual Basic.NET Programs ► ► Use debugging tools ► ► Set breakpoints and correct mistakes. ► ► Use a Watch and Local window to examine variables.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Lesson Two: Everything You Need to Know
Introduction To Matlab Class 9 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Chapter 6 Methods Chapter 6 - Methods.
Object Oriented Programming Criteria: P2 Date: 07/10/15 Name: Thomas Jazwinski.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Introduction To Matlab Class 5 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD.
Processing Variables. Variables Processing gives us several variables to play with These variables are always being updated and you can assume they have.
Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Matlab Class 6 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev.
DEPARTMENT MODULE User’s Guide. Step 1. Click Files Step 2. Click Department.
Matlab Class 8 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Error Analysis Logic Errors.
Area of Rectangles, Squares, Parallelograms, Triangles, and Trapezoids
Create a Halloween Computer Game in Scratch
MOM! Phineas and Ferb are … Aims:
Introduction To Matlab Class 3
Area of Polygons.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Matlab Class 2 Xiaotao Su, Ph. D
Using Procedures and Exception Handling
Explain what touch develop is to your students:
Introduction To Matlab Class 2
Introduction To Matlab Class 1
Matlab Class 6 Xiaotao Su, Ph. D
Introduction To Matlab Class 7
Introduction To Matlab Class 9
Objective The student will be able to:
Explain what touch develop is to your students:
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Creating a Simple Game in Scratch
“Captured screen” appears.
The storytelling icon game
Presentation transcript:

Matlab Class 4 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS) and Chris Kourtev

RuCCS Calling scripts within scripts This is done to modularize code Modular code is useful because you can –reuse the same piece of code in many different programs –have the same piece of code called many times in one program –Only have to debug that piece of code once and then be able to rely on it to work the same way all the time.

RuCCS calling scripts % my_prog.m j = 4; double_j if(j<7) double_j else half_j end disp(j) % double_j.m % doubles the value of j j = j*2; % half_j.m % cuts j in half j = j/2;

RuCCS calling scripts % my_prog.m j = 4; double_j if(j<7) double_j else half_j end disp(j) % double_j.m % doubles the value of j j = j*2; % half_j.m % cuts j in half j = j/2;

RuCCS Added benefit % my_prog.m j = 4; double_j if(j<7) double_j else half_j end disp(j) If double_j and half_j were much more complicated programs the benefit to seperating them out into separate scripts makes our code Shorter Simpler to read Less likely to have bugs You can also have your program perform largely different behaviors based upon different conditions.

RuCCS %draw_stuff.m clear all; screen_setup while( …) … screen(window, ‘FillRect’ … flip end clear screen %screen_setup ---Determine Operating System--- if(oldmac|win) Set up screen variables for old mac and windows else Set up screen for OS X end if(osx ==1) Screen(window,'Flip'); else Screen('CopyWindow', window, window_ptr); Screen('CopyWindow', blank, window); Screen(window_ptr, 'WaitBlanking'); end making programs for all versions of psychtoolbox

RuCCS Functions Functions are similar to scripts in that –they are separate from your main body of code –used to perform one coherent task –make your code neater Differences –You pass it specific variable(s) and it returns specific variables(s) –The variables within it are not accessible outside the function –The variables outside the function are not accessible inside the function

RuCCS %my_prog.m f = 4; k = double_me(f); i = 6; f = double_me(k); disp(f); disp(i); function d_val = double_me(i) %double_me.m %doubles any value passed to it d_val = i*2; Example of functions

RuCCS %my_prog.m f = 4; k = double_me(f); i = 6; f = double_me(k); disp(f); disp(i); function d_val = double_me(i) %double_me.m %doubles any value passed to it d_val = i*2; Example of functions Notice, i was set to 6 in my_prog, and i was used in double_me, but the two references didn’t effect eachother. Also each call to a function creates a separate set of variable references for that call. my_prog’s variables f = 4 then 16 k = 8 i = 6 double_me*’s variables i = 4 d_val = 8 double_me**’s variables i = 8 d_val = 16

RuCCS %my_prog2.m f = 9; [a, b] = double_times(f, 4); c = double_times(f, 4); disp(a); disp(b); disp(c); function [d_val, t_val] = double_times(i, fact) %double_times.m %doubles any value passed to it and multiplies d_val = i*2; t_val = i*fact; Multiple inputs/outputs

RuCCS For loops using different increments for i=1:10 disp(i); end 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 for i=1:2:10 disp(i); end 1, 3, 5, 7, 9 for i=2:2:10 disp(i); end 2, 4, 6, 8, 10for i=10:-2:1 disp(i); end 10, 8, 6, 4, 2 for i=10:-1:1 disp(i); end 10, 9, 8,7,6,5,4,3,2,1

RuCCS Task 1 create a function that will take any string and spell it backwards reverse_string(‘stressed’) returns ‘desserts’ for i=1:10 disp(i); end The length of a string is length(str_var) Tips: function d_val = double_me(i)

RuCCS clear all; screen_setup shape1_rect = [10, 10, 100, 100]; shape2_rect = shape1_rect + [100, 0, 100, 0]; dot_r = 20; %radius of cursor HideCursor; dot_color = [0, 0, 255]; shape1_color = [255, 0, 0]; shape2_color = shape1_color; tic while(toc<5) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse([window]); cursor = [ mouse_x-dot_r,... mouse_y-dot_r,... mouse_x+dot_r,... mouse_y+dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; end flip; end clear screen; ShowCursor;

RuCCS clear all; screen_setup shape1_rect = [10, 10, 100, 100]; shape2_rect = shape1_rect + [100, 0, 100, 0]; dot_r = 20; %radius of cursor HideCursor; dot_color = [0, 0, 255]; shape1_color = [255, 0, 0]; shape2_color = shape1_color; tic while(toc<5) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse cursor = [ mouse_x - dot_r,... mouse_y - dot_r,... mouse_x + dot_r,... mouse_y + dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; end flip; end clear screen; ShowCursor

RuCCS %clicking.m clear all; screen_setup shape1_rect = [10, 10, 100, 100]; shape2_rect = shape1_rect + [100, 0, 100, 0]; dot_r = 20; %radius of cursor HideCursor; dot_color = [0, 0, 255]; shape1_color = [255, 0, 0]; shape2_color = shape1_color; tic while(toc<8) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse; cursor = [mouse_x-dot_r,... mouse_y-dot_r,... mouse_x+dot_r,... mouse_y+dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; end flip; end clear screen; ShowCursor;

RuCCS Task 2 Make it so every time you click on the rectangles, they change color.

RuCCS >>> while(toc<5) Screen(window, 'FillRect',... shape1_color, shape1_rect); Screen(window, 'FillRect',... shape2_color, shape2_rect); [mouse_x, mouse_y, buttons] =... GetMouse([window]); cursor = [ mouse_x-dot_r,... mouse_y-dot_r,... mouse_x+dot_r,... mouse_y+dot_r]; Screen(window, 'FillOval',... dot_color, cursor); if(sum(buttons)>0) dot_color = [255*rand, 255*rand, 255*rand]; if(... (mouse_x>shape1_rect(1))&... (mouse_x< shape1_rect(3))&... (mouse_y< shape1_rect(2))&... (mouse_y< shape1_rect(4))... ) shape1_color = [rand*255, rand*255, rand*255]; end if(... (mouse_x>shape2_rect(1))&... (mouse_x<shape2_rect(3))&... (mouse_y<shape2_rect(2))&... (mouse_y<shape2_rect(4))... ) shape2_color = [rand*255, rand*255, rand*255]; end flip; end clear screen; ShowCursor;

RuCCS Task 3 Make the two squares move randomly around the screen bouncing off the walls. They should be still clickable.