for
for loop to repeat specified number
of times
Syntax
forindex=valuesstatementsend
Description
for executes a group of statements in a loop for a specified
number of times. index = values, statements,
endvalues has one of the
following forms:
initVal:endVal— Increment theindexvariable frominitValtoendValby1, and repeat execution ofstatementsuntilindexis greater thanendVal.initVal:step:endVal— Incrementindexby the valuestepon each iteration, or decrementsindexwhenstepis negative.valArray— Create a column vector,index, from subsequent columns of arrayvalArrayon each iteration. For example, on the first iteration,. The loop executes a maximum ofindex=valArray(:,1)ntimes, wherenis the number of columns ofvalArray, given bynumel(. The inputvalArray(1,:))valArraycan be of any MATLAB® data type, including a character vector, cell array, or struct.
Examples
Tips
To programmatically exit the loop, use a
breakstatement. To skip the rest of the instructions in the loop and begin the next iteration, use acontinuestatement.Avoid assigning a value to the
indexvariable within the loop statements. Theforstatement overrides any changes made toindexwithin the loop.To iterate over the values of a single column vector, first transpose it to create a row vector.
Extended Capabilities
Version History
Introduced before R2006a