strange outcome matlab-intern and command-line

Hi guys, something is bothering me and I can't get any clue how this problem can be solved: In my_m_file.m I generate a matrix of random numbers and save it:
N_max=200; M_max=16; alpha=(-1).^randi(2,[N_max,M_max]); save('alphafile.mat','alpha')
So when I run this code from inside Matlab, everything works fine and each time I run the code I get a different matrix. But, when I run this code from the (Ubuntu) command line, e.g. by
> cat my_m_file.m | matlab -nodesktop -nosplash
no errors occur, but I always get the same matrix! I tried this really couple of times, and very same result each run. I also wrote a script to run the code, as
#!/bin/bash ./test.sh matlab -nodisplay -nojvm -nosplash -nodesktop -r \ "try, run('<path_to_file>/my_m_file.m'), catch, exit(1), end, exit(0);"
but again: same result! I deleted the file "alphafile.mat" after each run to exclude the possibility that it could not have been overwritten. Please help me, what is the reason for this to happen, and even more important: how can this be solved??? Thanks a lot in advance!

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 20 de Mzo. de 2017
When MATLAB starts, the random number generator seed is the same ( rng('default') ). To reseed it to something random, put this at the beginning:
rng shuffle

3 comentarios

Michael Werther
Michael Werther el 20 de Mzo. de 2017
Brilliant, thank you so much for your prompt reply and this solution! I would never have taken sth like that into account. So one last question: the documentation says
rng('shuffle') seeds the random number generator based on the current time
- does this mean the time after matlab-startup or the computer-time?
Sean de Wolski
Sean de Wolski el 20 de Mzo. de 2017
Editada: Sean de Wolski el 20 de Mzo. de 2017
The operating system clock.
Michael Werther
Michael Werther el 13 de Abr. de 2017
Thanks, the program runs fine now!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Mzo. de 2017

Comentada:

el 13 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by