KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

FOR LOOPS

One of the principle reason we used programs is that they allow repetitive actions to be done over and over again easily.

Loop structures do just this. The basic format is:

For loop_counter = 1:n

     Command 1
     Command 2
     Command 3
End


This loop will do Command 1, Command 2, and Command 3 from 1 to n times where n is a variable that specifies the number of times the loop is repeated. The loop knows what iteration it is on because this is held in the variable loop_counter.

So, start a new script in MATLAB (with our usual starting commands) and try the following code:


clc;
clear all;

close all;

for loop_counter = 1:10                                                                    
    disp('Hello World!');                                                                   
end
 


What does this code do? It will write Hello World! 10 times to the Command Window.

Let's try another example. Try this script:


clc;
clear all;

close all;

x = 0;
for loop_counter = 1:10                                                                    
     x = x + 1;
     disp(['The current value of x is ' num2str(x)]);
end 

What does this code do? It will show 10 successive values of x. Note, we used the num2str command to convert x into a text string for writing.

Let's make one more adjustment to the code and see what it does:


clc;
clear all;

close all;

x = 0;
for loop_counter = 1:10                                                                    
     x = x + 1;
     disp(['On loop ' num2str(loop_counter) ' the current value of x is ' num2str(x)]);
end 

For understanding:
1. Modify the code above so that two numbers get incremented, x which goes up by 2 each time and y which goes up by 5 each time.
2. Make the loop repeat 100 times.

Code that does this can be found HERE.

Some things to note. First, you will note that the for and end commands do not require a semicolon at the end. Second, we typically indent the code within loops so we can clearly see what is in the loop and what is not.

Advanced Tip. The default increment of a loop counter is 1. However, you can specify any value you want or even count down if you wish. Try these two bits of code now.


clc;                                                                                        
clear all;                                                                                  
close all;                                                                                 

x = 0;                                                                                      
y = 0;                                                                                     

for loop_counter = 0:2:100                                                                   
     x = x + 2;                                                                             
     y = y + 5;                                                                             
     disp(['On loop ' num2str(loop_counter) ' x is ' num2str(x) ' and y is ' num2str(y)]);    
end   
   

And:

clc;                                                                                        
clear all;                                                                                  
close all;                                                                                 

x = 0;                                                                                      
y = 0;                                                                                     

for loop_counter = 100:-1:0                                                                
     x = x + 2;                                                                             
     y = y + 5;                                                                             
     disp(['On loop ' num2str(loop_counter) ' x is ' num2str(x) ' and y is ' num2str(y)]);    
end   

Move onto the next tutorial now.

  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops