Home > FMAToolbox > Database > private > RunBatch.m

RunBatch

PURPOSE ^

RunBatch - Run a batch job. This should *not* be called directly.

SYNOPSIS ^

function RunBatch(timer,event,b)

DESCRIPTION ^

RunBatch - Run a batch job. This should *not* be called directly.

 Run a batch job. This function is called automatically by the
 batch timer upon expiration of the required delay.

  USAGE

    RunBatch(timer,event,b)

    timer          Matlab timer object
    event          Matlab timer event type
    b              batch structure

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %RunBatch - Run a batch job. This should *not* be called directly.
0002 %
0003 % Run a batch job. This function is called automatically by the
0004 % batch timer upon expiration of the required delay.
0005 %
0006 %  USAGE
0007 %
0008 %    RunBatch(timer,event,b)
0009 %
0010 %    timer          Matlab timer object
0011 %    event          Matlab timer event type
0012 %    b              batch structure
0013 %
0014 
0015 % Copyright (C) 2007-2013 by Michaƫl Zugaro
0016 %
0017 % This program is free software; you can redistribute it and/or modify
0018 % it under the terms of the GNU General Public License as published by
0019 % the Free Software Foundation; either version 3 of the License, or
0020 % (at your option) any later version.
0021 
0022 function RunBatch(timer,event,b)
0023 
0024 if b.hide,
0025     status = Hide('status');
0026     Hide('on');
0027     warning('off','FMAToolbox:Hide:FigureHidden');
0028 end
0029 
0030 progress = ['########## 0/' int2str(length(b.field)) ' = 0%% done [starting] ##########\n'];
0031 fprintf(1,progress);
0032 if b.log ~= -1,
0033     try
0034         fprintf(b.log,progress);
0035     catch
0036         fprintf(2,[' (Progress information could not be saved to log file)\n']);
0037     end
0038 end
0039 
0040 tic;
0041 while true,
0042     % Get next item
0043     [b,item,line] = GetNextItem(b);
0044     if isempty(item), break; end
0045     % Start building command line
0046     i = 1;
0047     clear('args');
0048     while true,
0049         % Get next field
0050         [b,field] = GetNextField(b);
0051         if isempty(field), break; end
0052         args{i} = field;
0053         i = i + 1;
0054     end
0055 
0056     try
0057         % Call batch function
0058         outputs = cell(1,nargout(b.mfile));
0059         [outputs{:}] = feval(b.mfile,args{:});
0060         % Append results to 'UserData' field of timer object (this is a cell array)
0061         data = timer.UserData;
0062         if isempty(data),
0063             data = {outputs{:}};
0064         else
0065             data(end+1,:) = {outputs{:}};
0066         end
0067         timer.UserData = data;
0068     catch
0069         % Print error messages
0070         fprintf(2,['Batch: error processing item ' int2str(item) ' on line ' int2str(line) '\n']);
0071         e = lasterror;
0072         fprintf(2,[' ' e.message '\n']);
0073         for j = 1:length(e.stack),
0074             fprintf(2,[' Error in ==> ' e.stack(j).name ' at ' int2str(e.stack(j).line) '\n']);
0075             if strcmp(e.stack(j).name,b.mfile), break; end
0076         end
0077         % Log error messages
0078         if b.log ~= -1,
0079             try
0080                 fprintf(b.log,['Batch: error processing item ' int2str(item) ' on line ' int2str(line) '\n']);
0081                 fprintf(b.log,[' ' e.message '\n']);
0082                 for j = 1:length(e.stack),
0083                     fprintf(b.log,[' Error in ==> ' e.stack(j).name ' at ' int2str(e.stack(j).line) '\n']);
0084                     if strcmp(e.stack(j).name,b.mfile), break; end
0085                 end
0086             catch
0087                 fprintf(2,[' (Error messages could not be saved to log file)\n']);
0088             end
0089         end
0090     end
0091 
0092     % Progress information
0093     t = toc;
0094     proportion = item / length(b.field);
0095     left = t*(1/proportion-1);
0096     if left > 24*3600,
0097         left = strrep(datestr(datenum(0,0,0,0,0,left),'dd-HH:MM:SS'),'-','d ');
0098     else
0099         left = datestr(datenum(0,0,0,0,0,left),'HH:MM:SS');
0100     end
0101     progress = sprintf(['########## ' int2str(item) '/' int2str(length(b.field)) ' = %.2f%%%% done [' left ' left] ##########\n'],100*proportion);
0102     fprintf(1,progress);
0103     if b.log ~= -1,
0104         try
0105             fprintf(b.log,progress);
0106         catch
0107             fprintf(2,[' (Progress information could not be saved to log file)\n']);
0108         end
0109     end
0110 end
0111 
0112 % Elapsed time
0113 t = toc;
0114 done = sprintf('Batch finished in %f s.\n',t);
0115 fprintf(1,done);
0116 if b.log ~= -1,
0117     try
0118         fprintf(b.log,done);
0119     catch
0120         fprintf(2,[' (Elapsed time could not be saved to log file)\n']);
0121     end
0122 end
0123 
0124 
0125 if b.hide,
0126     Hide(status);
0127     if strcmp(status,'off'), Hide('none'); end
0128     warning('on','FMAToolbox:Hide:FigureHidden');
0129 end
0130

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005