nav-left cat-right
cat-right
Matthias Pospiech > Programming > matlab > waitbar with cancel button

waitbar with cancel button

The following code is an example for a loop function with a waitbar that allows canceling of the loop with the cancel button: Note however that a waitbar consumes very much time. Depending on the code this can slow down the calculation significantly.
if numel(indices) > 1
    hWait = waitbar(0,'1', ...
            'Name','Exporting Plots ...', ...
            'CreateCancelBtn', ...
            'setappdata(gcbf,''canceling'',1)');
    cleanupWaitbar = onCleanup( @()( delete( hWait )));
    setappdata(hWait,'canceling',0);    
else
    hWait = [];
end
 
for k = indices            
    if getappdata(hWait,'canceling')
        break
    end
 
    if ~isempty(hWait)
        waitStr = [num2str(k) ' / ' num2str(numel(indices))];
        waitbar(k/numel(indices),hWait,waitStr);
    end
 
    plotData(data{k});
end

Einen Kommentar schreiben

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *


*

This blog is kept spam free by WP-SpamFree.