MATLAB GUI stop button issues - matlab

EDIT: I rephrased my question a bit, because I have a better understanding of the problem now and there was a lot of unnecessary info in the first draft.
I am creating a standalone MATLAB application, which needs a toggle button that can initiate and stop a looping script.
Based on this helpful video, I was able to implement this idea like this in my gui.m file:
function startBtn_Callback(hObject, eventdata, handles)
if get(handles.startBtn,'Value')
set(handles.startBtn,'String','Stop Recording');
else
set(handles.startBtn,'String','Start Recording');
end
while get(handles.startBtn,'Value');
disp('looping..');
pause(.5);
end
This script works as expected, but when I replace the contents of the while loop the function I would like to loop, the button stops working. It still toggles when I push it, but the callback only gets called the first time the button is pushed. Here is what my final code looks like:
function startBtn_Callback(hObject, eventdata, handles)
if get(handles.startBtn,'Value')
set(handles.startBtn,'String','Stop Recording');
pause(.1);
else
set(handles.startBtn,'String','Start Recording');
disp('Recording Stopped')
end
while get(handles.startBtn,'Value');
myFunction();
end
When I push the start button, this callback runs and the loop starts. The pause(.1) is needed to get the text to change - if I don't include a pause, the loop initiates, but the text on the button does not change.
After this, no subsequent button pushes do anything. The button toggles on the GUI, but startBtn_Callback never gets called and the loop runs indefinitely. This is a problem because my end user will not have access to the MATLAB console.
To give a bit more information about my function: its a method that records audio for 5 seconds, does some processing, then outputs some graphs. I want this loop to repeat indefinitely until the user pushes stop.
I think that the issue is that MATLAB seems to only be able to run one function at a time, so when myFunction() is running, the callback can't be initiated. The reason it worked in the first example is because there was a pause between loop calls. I can't have this pause, because a requirement of the project is to record every possible second.
How can I make a reliable stop button for this process?
I am running MATLAB R2012b 32-bit.

In your code snippet
a=get(handles.startBtn,'Value')
while a
myFunction();
end
the value of a is assigned once, and never changes afterward. Thus, the while-loop will either never run, or it will loop forever.
while get(handles.startBtn,'Value')
myFunction();
end
will query the value of the button at every iteration, however, especially if myFunction doesn't take very long to execute, there will be lots and lots of java requests that may make your GUI sluggish.
This is why the example you followed uses the pause line, so that the loop only executes every .5 seconds or so.
If you don't want to have the query in the while-line, you can alternatively write
%# read inital value of a
a=get(handles.startBtn,'Value');
while a
myFunction();
%# update a
a=get(handles.startBtn,'Value');
end

drawnow() is the function I was looking for. Putting that after myFunction() forces Matlab to handle any stacked up GUI calls before proceeding with the loop.
This code creates a reliable start/stop toggle button for an indefinite and continuous process:
function startBtn_Callback(hObject, eventdata, handles)
if get(handles.startBtn,'Value')
set(handles.startBtn,'String','Stop');
drawnow();
else
set(handles.startBtn,'String','Start');
end
while get(handles.startBtn,'Value');
myFunction();
drawnow()
end

Related

MATLAB GUI hangs up despite using drawnow and pause

I have a MATLAB GUI that looks as shown in:
MATLAB GUI image
What I am trying to achieve is that MATLAB keep checking for midnight continuously, except for the pauses when the user makes any change to the interface. Hence, I am running a while loop in the background continuously as I need to check if it is midnight. If it is, I perform some functions. The function that contains this while loop is called after any user input change is detected i.e. at the end of all the callback functions for the pop-up menus,pushbuttons, textboxes etc. This is the reason I had the drawnow in the while loop, so that if the user makes any changes and wants to run some calculations, that will be detected. After the completion of the calculations, I again call the function which has this while loop.
The issue is, even though I use drawnow and pause in my while loop, sometimes, not always, MATLAB still hangs-up on me and the GUI becomes unresponsive and does not recognize any of the user inputs. Here is the while loop part of my code:
while 1
pause(0.1);
drawnow;
pause(0.1);
current_time=clock;
if current_time(4)==0
post_mortem;
end
end
I know the above code is not efficient as it will call post_mortem continuously in the midnight hour, that however is not my issue right now. My issue is that it hangs up on me at times even at noon for example. Does anybody have any solution to this? On searching for answers to previous similar questions, the solution seemed to be to use drawnow and pause, but that doesn't seem to be working for me either.
Any guidance would be appreciated.
Thank you
Since MATLAB is not multi-threaded, using a while loop to continuously check something (such as the time) is going to cause all sorts of blocking of other functionality. While drawnow and pause can potentially help with this, there are still some potential issues that can crop up.
A more elegant and reliable approach would be to use a timer object to check the time at a pre-specified interval. This way, any user interaction with the GUI will automatically be registered and any callbacks will execute without you having to call either pause or drawnow.
You can create and start the timer as soon as you create your GUI.
% Create the timer object
handles.mytimer = timer('ExecutionMode', 'FixedRate', ...
'Period', 1/5, ...
'BusyMode', 'drop', ...
'TimerFcn', #(s,e)timerCallback());
% Start the timer
start(handles.mytimer)
function timerCallback()
% Callback that executes every time the timer ticks
current_time = clock;
if current_time(4) == 0
post_mortem;
end
end

Pausing MATLAB program for GUI

I have a code which does some calculations, and then calls a GUI. In the GUI I have some options for user to select from. Based on that selection, further calculations are done.
Function this = random(this)
... some calculations
GUI % calls GUI
value = getappdata(0,'value') % Gets value from GUI
... some calculations
end
The problem is when the code is run, it calls the GUI and goes ahead with the program, how do I make the program wait until the selection in GUI is made, or rather the GUI is closed?
You'll want to use either waitfor(GUI) or uiwait(GUI) depending on what happens when GUI is called. waitfor is if GUI is an objecthandle and uiwait is if GUI returns a figure handle.

Moving control out of a loop when user presses a specific key? [duplicate]

This question already has answers here:
Detect Keyboard Input Matlab
(4 answers)
Closed 6 years ago.
I am using an infinite loop in my code.
while 1
% many statements/computation...
...
...
end
I want my control to come out of the loop when a user presses a specific key, say Spacebar.
I've tried the solution given here but no luck. Figure window does not open.
You have two routes you can take.
1) You can take even-driven approach and register KeyPressFcn callback for the current figure. The catch is that Matlab is single threaded*, so as long as your loop continuously executes some code, event will not be handled. A workaround would be to insert either drawnow or pause statements within your loop. That will halt the main thread execution and would process outside events, such as our key press. In the callback you would need to change the state of some flag, e.g. an object property / appdata of some graphics handle, or even a global variable (which I would advise against). You would then be able to query from within the loop and break if you see the flag changing.
function test()
hFig = figure();
setappdata(hFig, 'space_pressed', false);
set(hFig, 'KeyPressFcn', #keyPressedFcn);
while true
A = randn(10000); % doing some stuff
pause(0.01);
flag = getappdata(hFig, 'space_pressed');
if flag
break;
end
fprintf('Still running...\n');
end
fprintf('Finished!\n');
end
function keyPressedFcn(hFig, event)
fprintf('Pressed character: "%s"...\n', event.Character);
if strcmp(event.Character, ' ')
setappdata(hFig, 'space_pressed', true);
end
end
Sample output:
>> test
Still running...
Still running...
Pressed character: "h"...
Still running...
Still running...
Pressed character: " "...
Finished!
2) Second approach would be to poll for CurrentCharacter. Note that it also requires you to halt execution by using pause / drawnow for the character stroke to be processed. A side-effect of this approach is that after a key press the focus moves to command window, and the character also appears there.
function test()
hFig = figure();
set(hFig, 'CurrentCharacter', '^'); % some other character
drawnow();
while true
A = randn(10000); % doing some stuff
pause(0.01);
char = get(hFig, 'CurrentCharacter');
if strcmp(char, ' ')
break;
end
fprintf('Still running...\n');
end
fprintf('Finished!\n');
end
EDIT elaborating on the threading in Matlab:
Matlab GUI components actually run on EDT thread, which is separate to the one where the main execution is happening (unless you create java GUI objects explicitly without using javaObjectEDT, which you should avoid) - this is what allows GUI components to visually react on some actions you take which do not require synchronization with the main execution thread, like visually changing button state when clicking on it. But to actually get the callback to run and change the state of your workspace inside the execution thread, you need to let Matlab synchronize the two threads. This is what pause/drawnow helps to do - it halts whatever you've been doing on the execution thread which allows your GUI callbacks to execute in the meantime. drawnow would clear the whole pending queue before returning execution to your original code block, while pause will potentially only process some of the callbacks until the time runs out. You may observe this if you set pause argument to a tiny fraction of a second and make lots of actions - not all will be processed.
You may notice some slight changes in behavior between different Matlab releases - the two threads seem to become slightly more independent over time. There is surprisingly little official information available on this - most of it is based on experience and various answers scattered across Matlab central, like this and that, and obviously fantastic articles on the UndocumentMatlab, like the one linked above.
Why not something like that:
ended = false
while !ended
if pressed_spacebar
ended = true
If you can't change your loop condition, you can use the break instruction but it's not a good programming practise :)

Stop A GUI in a middle of process in MATLAB

I'm designing a GUI using GUIDE in MATLAB R2014b. My program has a long loop (takes 2~5h to process). I want have a button in my GUI that the user stop the process every time he/she want (The GUI is updating graphs and texts continuously based on result of loops). Something like pressing Control+C not after ending a loop. How can I implement this?
PS.
I don't want MATLAB remove my workspace. The user can continue the process with previous loaded data and workspace by changing some options in GUI.
Here is a trick that should work: Somewhere in the GUI, like in its OpeningFcn for instance, initialize a flag named for example StopNow to false and store it in the handles structure of the GUI. Then in the loop that takes long to execute, put an if statement with a call to return whenever the flag is set to true. That will stop the execution of the loop and you will have access to your data. You can make a pushbutton to change the flag value.
Sample code: I made a simple GUI that starts enumerating digits in a for loop and printing them in a text box. When you press the STOP button, the flag is set to true and the loop stops. If something is unclear please tell me.
function StopGUI
clear
clc
close all
%// Create figure and uielements
handles.fig = figure('Position',[440 500 400 150]);
handles.CalcButton = uicontrol('Style','Pushbutton','Position',[60 70 80 40],'String','Calculate','Callback',#CalculateCallback);
handles.StopButton = uicontrol('Style','Pushbutton','Position',[250 70 80 40],'String','STOP','Callback',#StopCallback);
%// Initialize flag
handles.StopNow = false;
handles.Val1Text = uicontrol('Style','Text','Position',[150 100 60 20],'String','Value 1');
handles.Val1Edit = uicontrol('Style','Edit','Position',[150 70 60 20],'String','');
guidata(handles.fig,handles); %// Save handles structure of GUI. IMPORTANT
function CalculateCallback(~,~)
%// Retrieve elements from handles structure.
handles = guidata(handles.fig);
for k = 1:1000
if handles.StopNow == false
set(handles.Val1Edit,'String',num2str(k));
pause(.5) %// The pause is just so we see the numbers changing in the text box.
else
msgbox('Process stopped');
return
end
end
guidata(handles.fig,handles); %// Save handles structure of GUI.
end
function StopCallback(~,~)
%// Retrieve elements from handles structure.
handles = guidata(handles.fig);
handles.StopNow = true;
guidata(handles.fig,handles); %// Save handles structure of GUI.
end
end
Screenshot after pressing the STOP button:
Hope that helps!
Best solution would be to use separate threads (one for the user interface and one for the processing) maybe using parallel toolbox. Anyhow this would be then quite complex to synchronise both.
So, here is a simple solution that only relies on anonymous functions (to delegate interface refreshing outside the processing block) and on drawnow function (to force the graphical interface to process its messages).
Sample application
The sample application to work with is very basic. It contains:
A settings panel (with only one setting, the number of iterations for the processing block)
A progress bar
A Go/Cancel button
NB: Source code is quite long (about 250 lines, mainly due to gui creation), so I dropped it here.
GUI creation is not important. The most important parts are the processing block, the anonymous functions to instrument the code and the callbacks to react to the GUI events. I will thus detail theses only.
The processing block
Processing block is a simple routine:
function [] = processing(count, instrumentation)
%[
for ki = 1:count,
instrumentation.CheckCancel();
instrumentation.Progress((ki-1)/count);
if (ki > 1), pause(1); end
instrumentation.CheckCancel();
instrumentation.Progress(ki/count);
end
%]
end
The only special thing about it is that it takes an additional instrumentation structure. This structure has two fields that points to two anonymous functions defined by the caller (i.e. the graphical interface). We'll see shortly how.
CheckCancel is a function in charge of raising an error if the user wants to stop the processing.
Progress is a function on which to delegate progression report.
Here is how the anonymous functions are connected to the graphical interface (see doProcessing sub-function in the code):
% Connect instrumentation callbacks with the gui
instrumentation.CheckCancel = #(ratio)onCheckCancel(dlg);
instrumentation.Progress = #(ratio)onProgress(dlg, ratio);
% Perform the processing
processing(settings.NumberOfIterations, instrumentation);
Progress and CheckCancel handlers
Here is the handler defined by the GUI for Progress:
function [] = onProgress(dlg, ratio)
%[
% Update the progress bar value
data = guidata(dlg);
uiprogress(data.handles.pbProgress, ratio);
% Force interface to refresh
drawnow();
%]
end
This is simple, it just updates progressbar control and forces the GUI to refresh (remind that matlab is single-threaded and is currently executing the processing block, so we need to force GUI refreshing).
Here is the handler for CheckCancel:
function [] = onCheckCancel(dlg)
%[
% Force interface to process its events
drawnow();
% Check 'UserData' has not been modified during events processing
guiState = get(dlg, 'UserData');
if (~isempty(guiState) && ....
strcmp(guiState, 'CancelRequested') || strcmp(guiState, 'CloseRequested'))
error('System:OperationCanceledException', 'Operation canceled');
end
%]
end
Again, this is quite simple. We force the GUI to process events (buttons clicks, etc...) and then we read if UserData was modified to some expected value. If so, we raise an exception to stop the processing. Remind again that currently executing code is the processing block.
GUI events handlers
The GUI has only two interesting events. Either the user clicks the close button, either he/she clicks the Go/Cancel button.
NB: Remind that even if matlab is locked in executing the processing block, GUI events are still processed because processing block is calling drawnow from time to time (by the mean of the instrumentation delegates).
Here is the code for the close button:
function [] = onCloseRequest(dlg)
%[
% If already in computation mode
if (~isempty(get(dlg, 'UserData')))
% Just indicate close is requested and leave immediatly
set(dlg, 'UserData', 'CloseRequested');
data = guidata(dlg);
set(data.handles.btnGoCancel, 'Enable', 'off', 'String', 'Cancelling...');
return;
end
% Immediate close
delete(dlg);
%]
end
This is simple, if we are in running mode, we just signal we want to stop, else we close the dialog immediately.
Here is the code for the go/cancel button:
function [] = onGoCancelClick(dlg)
%[
% If already in computation mode
if (~isempty(get(dlg, 'UserData')))
% Just indicate cancel is requested and leave immediatly
set(dlg, 'UserData', 'CancelRequested');
data = guidata(dlg);
set(data.handles.btnGoCancel, 'Enable', 'off', 'String', 'Cancelling...');
return;
end
% Go into computation mode
[settings, err] = tryReadSettings(dlg);
if (~isempty(err))
waitfor(msgbox(err.message, 'Invalid settings', 'Error', 'Modal'));
else
enterComputationMode(dlg);
err = doProcessing(dlg, settings);
leaveComputationMode(dlg, err);
end
%]
end
It's a little longer, anyhow this is the same. If we're in running mode, we just indicate we want to stop; else, the interface is in normal mode, and we start the processing.
Functions tryReadSettings, enterComputationMode and leaveComputationMode are just glue to update controls in the interface and nicely report for errors or cancellation.
Conclusion
We have designed a responsive graphical interface relying only on drawnow and anonymous functions. This is of course just a trick and a better solution would be to use multitasking.
The graphical interface was created here programmatically. Principle is the same if build with GUIDE or with the help of the GUI Layout toolbox.
Instrumentation callbacks can further be improved, for instance, by adding a Log field to report processing details in a textbox or to some back-end similar to Log4Net (with just a message level and message value). Or by adding callback for intermediate results.
Interface can also be improved or modified, for instance why not to run processing whenever a setting is modified (i.e. just stopping any current run and without the need to manually click on a 'Go/Cancel' button each time).
There are a lot of possibilities. Here, just providing some ground application to start with (or not ...).

How to specify when to return output of the function

Say I have the following function:
function result=myfun(varargin)
result=[];
myFig=figure();
B1=uicontrol(myFig,'Style','pushbutton','String','done','Callback',{#done_Callback});
function done_Callback(varargin)
result =10;
delete(mainFig);
end
end
I am trying to only return output after the button callback is executed. Right now it returns an empty variable right away. I know how to do this in guide GUIs but all of my project is written without guide. I am assuming I need uiwait somewhere, but not sure where.
The function uicontrol only generates the button, it doesn't wait for it to be pressed. Otherwise, a full GUI with several elements would not be possible – you couldn't insert another element before the first hadn't been activated, and afterwards the first one could not be activated any longer. For this reason, GUI callbacks are executed in another thread than the main Matlab program, namely in the "event queue".
If you want your program to wait until the button is pressed, you have to program this explicitly:
function result=myfun(varargin)
result=[];
myFig=figure();
B1=uicontrol(myFig,'Style','pushbutton','String','done','Callback',{#done_Callback});
while isempty(result)
drawnow
end
function done_Callback(varargin)
result =10;
fprintf('hi\n')
delete(myFig);
end
end
The drawnow is there to tell Matlab it should allocate execution time to the event queue, which is not normally done when Matlab is busy, e.g. with running a while loop.
For (slightly) more information, have a look at How Does a GUI Work? and drawnow.