MATLAB Answers

# How do I create a for loop in MATLAB?

7,024 ビュー (過去 30 日間)
pedro marin 2012 年 3 月 5 日

I am completely lost in for loops, I just don't get it. The book and my professor haven't helped much. Where can I get help?

#### 2 件のコメント

Abderrahmane Walid AISSANI 2017 年 5 月 14 日
hi I am trying to create several variables using the for loop
% for k=1:1:10
tab_k = csvread('');
end
in fact the code is wrong but I want to iterate the "k" in the name of the variable but I can't find how to do that .
thank you .
Walter Roberson 2017 年 5 月 14 日

サインインしてコメントする。

### 採用された回答

Walter Roberson 2012 年 3 月 5 日

A basic for loop in MATLAB is often used to assign to or access array elements iteratively. For example, let’s say you have a vector A, and you want to simply display each value one at a time:
A = [3 6 9 4 1];
for i = 1:length(A)
disp(A(i))
end
For more examples using for loops, see:

#### 1 件のコメント

Patrik Ek 2015 年 4 月 9 日
As far as I know the for-loop is even more excellent than what says in this text. As far as I know the matlab for-loop works similar to the for_each-loop that can be seen in many programming languages these days, or maybe the range-for in c++11. For example is this possible,
a = aObjArray;
for k = a
a.modify();
end
This is not really clear from the documentation, so I thought this would be a valuable comment.

サインインしてコメントする。

### その他の回答 (3 件)

Jan 2012 年 3 月 5 日
You can get help from the documentation of Matlab:
doc for
help for
There you find examples and explanations.

#### 1 件のコメント

Meenakshi Bhardwaj 2018 年 7 月 19 日
Thanks so much. Finally, I understood what is for loop.

サインインしてコメントする。

Jan Afridi 2017 年 9 月 29 日
For loop repeat itself for a given number of input. The syntax for “For Loop Matlab” is
for variable = expression
Program Statement
end
In the above syntax, the expression has one of the following forms.
Initial value : Final value
for x = 1:10
fprintf('value of x: %d\n', x);
end
Initial value : Step : Final value
for x = 1:2:10
fprintf('value of x: %d\n', x);
end
Value Array
for x = [1 4 6 8 90]
disp(x)
end
To learn more about in detail visit this link For Loop Matlab

#### 0 件のコメント

サインインしてコメントする。

Annesha Ahsan 2019 年 1 月 4 日
for i=1:2:10
UnablEto get the answer

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by