A very odd loop behavior

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.

 Respuesta aceptada

Tomas Iesmantas
Tomas Iesmantas el 14 de Mzo. de 2013

0 votos

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.

2 comentarios

Jan
Jan el 14 de Mzo. de 2013
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.
Tomas Iesmantas
Tomas Iesmantas el 20 de Mzo. de 2013
In my case the content of the function varies:number of equations, complexity of equation, parameters of that equation. So it is not possible to creat one function and just feed the parameters. However, I found way better solution - creating the function as a string and not printing it to some m file, but converting it to function with matlab function "str2func".

Iniciar sesión para comentar.

Más respuestas (1)

Wayne King
Wayne King el 14 de Mzo. de 2013

0 votos

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

4 comentarios

Tomas Iesmantas
Tomas Iesmantas el 14 de Mzo. de 2013
Yes, the "Function()" is just my function for specific calculations. I assume that the such behavior is triggered by my function. But I would expect to be the same effect for subsequent calls as for "for" loop.
Wayne King
Wayne King el 14 de Mzo. de 2013
I would expect so too, but if the function is not too big, you should post it because nobody can help you further without it.
Tomas Iesmantas
Tomas Iesmantas el 14 de Mzo. de 2013
Unfortunately the function is very complex and uses another subfunctions as well as construction of txt files, so it would not be of any use to post it here.
Wayne King
Wayne King el 14 de Mzo. de 2013
Understood

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by