Home > FMAToolbox > Database > DebugBatch.m

DebugBatch

PURPOSE ^

DebugBatch - Help debug a batch job.

SYNOPSIS ^

function DebugBatch(mfile,bfile,item,varargin)

DESCRIPTION ^

DebugBatch - Help debug a batch job.

  This function can be used to help debug a batch. It retrieves the parameters
  corresponding to the required item from the batch file, and either starts
  the batch function with these parameters in debug mode, or simply assigns
  these parameters to new variables in matlab's workspace so they can be
  directly manipulated.

  Set breakpoints in the batch function before running DebugBatch. If no
  breakpoints have been defined, DebugBatch will stop at the first line.

  USAGE

    DebugBatch(mfile,bfile,item,<options>)

    mfile          batch function (M-file name or function handle)
    bfile          batch file listing the parameters for each iteration
    item           item number in batch function
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'mode'        run the batch function ('run', default) or simply set
                   variables in matlab's workspace ('set')
    =========================================================================

  OUTPUT

    Instantiates the variables of the batch function using the values in the
    batch file ('set' mode).

  SEE

    See also StartBatch.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function DebugBatch(mfile,bfile,item,varargin)
0002 
0003 %DebugBatch - Help debug a batch job.
0004 %
0005 %  This function can be used to help debug a batch. It retrieves the parameters
0006 %  corresponding to the required item from the batch file, and either starts
0007 %  the batch function with these parameters in debug mode, or simply assigns
0008 %  these parameters to new variables in matlab's workspace so they can be
0009 %  directly manipulated.
0010 %
0011 %  Set breakpoints in the batch function before running DebugBatch. If no
0012 %  breakpoints have been defined, DebugBatch will stop at the first line.
0013 %
0014 %  USAGE
0015 %
0016 %    DebugBatch(mfile,bfile,item,<options>)
0017 %
0018 %    mfile          batch function (M-file name or function handle)
0019 %    bfile          batch file listing the parameters for each iteration
0020 %    item           item number in batch function
0021 %    <options>      optional list of property-value pairs (see table below)
0022 %
0023 %    =========================================================================
0024 %     Properties    Values
0025 %    -------------------------------------------------------------------------
0026 %     'mode'        run the batch function ('run', default) or simply set
0027 %                   variables in matlab's workspace ('set')
0028 %    =========================================================================
0029 %
0030 %  OUTPUT
0031 %
0032 %    Instantiates the variables of the batch function using the values in the
0033 %    batch file ('set' mode).
0034 %
0035 %  SEE
0036 %
0037 %    See also StartBatch.
0038 %
0039 
0040 % Copyright (C) 2007-2013 by Michaƫl Zugaro
0041 %
0042 % This program is free software; you can redistribute it and/or modify
0043 % it under the terms of the GNU General Public License as published by
0044 % the Free Software Foundation; either version 3 of the License, or
0045 % (at your option) any later version.
0046 
0047 % Default values
0048 mode = 'run';
0049 
0050 % Check number of parameters
0051 if nargin < 3,
0052     error(['Incorrect number of parameters (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).']);
0053 end
0054 
0055 % Batch function name
0056 if isa(mfile,'function_handle'),
0057     mfile = func2str(mfile);
0058 end
0059 
0060 % Check batch file and function are valid
0061 if ~isastring(bfile) || ~exist(bfile,'file'),
0062     error(['Batch file not found (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).']);
0063 end
0064 if isempty(which(mfile)),
0065     error(['Batch function not found (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).']);
0066 end
0067 
0068 % Item
0069 if ~isiscalar(item),
0070     error(['Incorrect item (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).']);
0071 end
0072 
0073 % Parse parameter list
0074 for i = 1:2:length(varargin),
0075   if ~ischar(varargin{i}),
0076     error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).']);
0077   end
0078   switch(lower(varargin{i})),
0079     case 'mode',
0080       mode = lower(varargin{i+1});
0081       if ~isastring(mode,'run','set'),
0082         error('Incorrect value for property ''mode'' (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).');
0083       end
0084     otherwise,
0085       error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help DebugBatch">DebugBatch</a>'' for details).']);
0086   end
0087 end
0088 
0089 % Open batch file
0090 f = fopen(bfile,'r');
0091 if f == -1, error(['Could not open file ''' bfile '''.']); end
0092 
0093 % Parse batch file
0094 b = ParseBatch(bfile);
0095 
0096 if strcmp(mode,'run'),
0097 
0098     % Check breakpoints
0099     if length(dbstatus(mfile)) == 0,
0100         eval(['dbstop in ' mfile ' at 1;']);
0101     end
0102     
0103     % Run batch function with appropriate parameters
0104     for i = 1:size(b.field,2),
0105         args{i} = b.field{item,i};
0106     end
0107     outputs = cell(1,nargout(mfile));
0108     [outputs{:}] = feval(mfile,args{:})
0109 
0110 else
0111 
0112     % Open batch function
0113     if isa(mfile,'function_handle'),
0114         mfile = func2str(mfile);
0115     end
0116     f = fopen(which(mfile));
0117 
0118     % Find first line containing the (uncommented) 'function' keyword
0119     found = [];
0120     while isempty(found),
0121         line = fgets(f);
0122         if line == -1, error('Could not find function definition in batch function.'); end
0123         line = regexprep(line,'%.*function.*','');
0124         found = regexp(line,'.*function[^(]*\(([^)]*)\).*');
0125     end
0126     fclose(f);
0127 
0128     % Extract parameter names, and assign them in 'base' workspace
0129     parameters = regexprep(line,'.*function[^(]*\(([^)]*)\).*','$1');
0130     parameters = regexp(parameters,'[^,]*','match');
0131     for i = 1:length(parameters),
0132         assignin('base',parameters{i},b.field{item,i});
0133     end
0134     
0135 end

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