Pausing an application in GUI
Mostra commenti meno recenti
I'm trying to make easy GUI for Game of Life. The problem is I don't really know, how can I implement pausing by pressing a button. It works with a check-box, but I'd prefer Pause button instead.
function gameoflife_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
set(gcf,'name','Conway''s Game Of Life');
% Choose default command line output for untitled
handles.output = hObject;
handles.iter_num = get(handles.iterations,'Value');
handles.iter_step = 1;
handles.size = get(handles.grid_size,'Value');
handles.vis_grid = zeros(30);
handles.vis_grid(rand(30)>0.5) = 1;
handles.paused=true;
% Update handles structure
guidata(hObject, handles);
plot_grid(handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
function startButton_Callback(hObject, eventdata, handles)
% hObject handle to startButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.paused = false;
start_app(handles);
end
function pause_Callback(hObject, eventdata, handles)
% hObject handle to pause (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.paused = ~handles.paused;
if handles.paused
set(hObject, 'String', 'Resume');
else
set(hObject, 'String', 'Pause');
end
guidata(hObject, handles);
end
function [] = start_app(handles)
it = 0;
while it < handles.iter_num
if ~handles.paused
handles.vis_grid = change_state(handles.vis_grid,neighbors(handles.vis_grid));
plot_grid(handles);
it = it + 1;
pause(1)
else
waitfor(handles.paused)
end
end
end
It there a way to do it?
Risposte (1)
Geoff Hayes
il 24 Mar 2017
Mykhaylo - rather than using a while loop to update the board, consider using a timer. Your start button would create the timer and start it and then your pause button could stop the timer or start it again depending upon the state of the timer. For example
function startButton_Callback(hObject, eventdata, handles)
% call function to start timer
startTimer(hObject, handles);
function pause_Callback(hObject, eventdata, handles)
if strcmpi(get(hObject, 'String),'Resume')
set(hObject, 'String', 'Pause');
startTimer(hObject, handles);
else
set(hObject, 'String', 'Resume');
stopTimer(hObject, handles);
end
function startTimer(hObject, handles)
handles.timer = timer('Name','MyTimer', ...
'Period',1, ...
'StartDelay',0, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
guidata(hObject,handles);
start(handles.timer);
function stopTimer(hObject, handles)
if isfield(handles,'timer') && ~isempty(handles.timer)
stop(handles.timer);
handles.timer = [];
guidata(hObject,handles);
end
The above code should allow you to start and stop a timer through the GUI buttons. Your timer callback would then have the code that updates the board
function [] = timerCallback(~,~,guiHandle)
if ~isempty(guiHandle)
% get the handles
handles = guihandles(guiHandle);
handles.vis_grid = change_state(handles.vis_grid,neighbors(handles.vis_grid));
plot_grid(handles);
guidata(handles.figure1, handles);
end
See https://www.mathworks.com/matlabcentral/answers/178682-collecting-a-variable-every-two-minutes for some reasoning behind the above code.
Categorie
Scopri di più su Conway's Game of Life in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!