What I have already figured out, is that since at every step in the "for" loop I create a new .m file and delete previous file, matlab still have a copy of the old file and executes that old .m instead of newly created. To overcome this I wrote a line to clear old .m file from a memory and everything seems to work properly.
A very odd loop behavior
1 次查看(过去 30 天)
显示 更早的评论
I encountered a quite odd and unseen behavior in "for" loops. Consider this simple loop:
for i=1:5
Function(rand(1,1))
end
The loop gives the same results, while repeated lines as this:
Function(rand(1,1))
Function(rand(1,1))
Function(rand(1,1))
Function(rand(1,1))
Function(rand(1,1))
Gives 5 different results. What might be the problem? It seems that the "for" loop evaluates the function 5 times with the same argument. This effect is in Wondows, while in Mac system everything goes smoothly and produces different results.
Any advice would be of great value.
0 个评论
采纳的回答
Tomas Iesmantas
2013-3-14
2 个评论
Jan
2013-3-14
Creating M-file dynamically is a bad programming practize. It is much more efficient and less prone to errors to call a static function with changing parameters. Any kind of meta-programming increases the effort for debugging substantially, and in all cases I've seen already, the parameterized functions have been more direct and clear.
更多回答(1 个)
Wayne King
2013-3-14
We don't know anything about Function(), but that is certainly not the case in Windows in general
for ii = 1:5
sum([rand(1,1) 0])
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!