strange outcome matlab-intern and command-line

3 views (last 30 days)
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!

Accepted Answer

Sean de Wolski
Sean de Wolski on 20 Mar 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 Comments
Sean de Wolski
Sean de Wolski on 20 Mar 2017
Edited: Sean de Wolski on 20 Mar 2017
The operating system clock.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!