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