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