MATLAB: How to set a global random seed in a GUI

globalguiMATLABrandom number generatorrngseed

Can you just do something like handles.rng('default') and then handles.rng(1) to initialize a seed and reinitialize the same seed later in the code?
Not sure how to go about this. I need a global random seed that I can reinitialize (with the same seed) in different callback functions (and functions called within callback functions).

Best Answer

You're on the right track. Choose a random seed when the GUI initializes and store it in your GUI using guidata() so all components have access to it and can reseed it whenever necessary.
Here's a demo. Based on your examples, I'm assuming you're not using app designer.
% At the end of the GUI opening function
function myOpeningFcn(hObject, . . .)
. . .
handles.rngSeed = randi(2^32 - 1);
guidata(hObject, handles);
end
% Some callback function
function myCallbackFcn(. . ., handles)
. . .
rng(handles.rngSeed)
end
Another good idea would be to add the following line to your closereq function so that your rng seed is randomized again.
rng shuffle