Matlab Continuing Interaction at End of Loop

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.

ResponsiveGuiInterface

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 ...).

mckinneythants.blogspot.com

Source: https://stackoverflow.com/questions/28364243/stop-a-gui-in-a-middle-of-process-in-matlab

0 Response to "Matlab Continuing Interaction at End of Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel