How can I get the variable from inside of the for loop and use it /display it after for loop?

4 次查看(过去 30 天)
Hi,
I created a for loop. Inside this for loop I calculated a variable. Unfortunately Matlab deletes the variable after the end of the for loop. How can I use the calculated variable outside of the for loop? Thanks!

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2012-10-26
编辑:Azzi Abdelmalek 2012-10-26
That depends where your loop is, in script file or function file If it's in Function file, that have nothing to do with a loop , you declare your variable global, If it's in the script file, your variable can't be deleted. look at these examples
for k=1:10
x=sin(k)
end
the result is x=sin(10); the previous x where erased, if you want to save them
for k=1:10
x(k)=sin(k)
end
  3 个评论

请先登录,再进行评论。

更多回答(1 个)

Sachin Ganjare
Sachin Ganjare 2012-10-26
Declare that variable as global, so that matlab won't delete it.
  4 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by