Site icon TestingDocs.com

For Loop Example in MATLAB

Introduction

For loop is an iterative loop in MATLAB. It is used to repeat and execute a set of instructions over and over for predefined number of times. Loop variable is used to track the current iteration. In simple terms, we can use the for loop in the MATLAB scripts/function if we know the number of iterations.

for loop syntax

for loopVariable =  loopStart:loopEnd

loop statements   % Iterative loop statements

end

Sample Script

% MATLAB For loop Demo Script.
% www.TestingDocs.com

clc;
clear;

fprintf("This loop runs for 10 times \n");
for i = 1:10
    fprintf("Loop variable value : %d \n", i);
end

Script Output

This loop runs for 10 times
Loop variable value : 1
Loop variable value : 2
Loop variable value : 3
Loop variable value : 4
Loop variable value : 5
Loop variable value : 6
Loop variable value : 7
Loop variable value : 8
Loop variable value : 9
Loop variable value : 10

 

Exit mobile version