Home > FMAToolbox > Database > private > ParseBatch.m

ParseBatch

PURPOSE ^

ParseBatch - Parse batch file.

SYNOPSIS ^

function b = ParseBatch(bfile)

DESCRIPTION ^

ParseBatch - Parse batch file.

  USAGE

    b = ParseBatch(bfile)

    bfile          batch file listing the parameters for each iteration

  SEE

    See also StartBatch, ShowBatch, InitBatch.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function b = ParseBatch(bfile)
0002 
0003 %ParseBatch - Parse batch file.
0004 %
0005 %  USAGE
0006 %
0007 %    b = ParseBatch(bfile)
0008 %
0009 %    bfile          batch file listing the parameters for each iteration
0010 %
0011 %  SEE
0012 %
0013 %    See also StartBatch, ShowBatch, InitBatch.
0014 %
0015 
0016 % Copyright (C) 2007-2013 by Michaƫl Zugaro
0017 %
0018 % This program is free software; you can redistribute it and/or modify
0019 % it under the terms of the GNU General Public License as published by
0020 % the Free Software Foundation; either version 3 of the License, or
0021 % (at your option) any later version.
0022 
0023 % Check number of parameters
0024 if nargin < 1,
0025     error('Incorrect number of parameters (type ''help <a href="matlab:help ParseBatch">ParseBatch</a>'' for details).');
0026 end
0027 
0028 % Make sure batch file is valid
0029 s = dbstack;
0030 if ~isastring(bfile) || ~exist(bfile,'file'),
0031     error(['Batch file not found (type ''help <a href="matlab:help ' s.name '">' s.name '</a>'' for details).']);
0032 end
0033 
0034 % Open batch file
0035 f = fopen(bfile,'r');
0036 if f == -1, error(['Could not open file ''' bfile '''.']); end
0037 
0038 item = 1;
0039 l = 1;
0040 nFields = 0;
0041 b.field = {};
0042 % Read batch file
0043 while ~feof(f),
0044     field = 0;
0045     % Read next line
0046     line = fgetl(f);
0047     while ~isempty(line),
0048         % Get next field
0049         [token,line] = strtok(line);
0050         % Skip rest of line if this is a comment
0051         if isempty(token) | token(1) == '%', break; end
0052         field = field + 1;
0053         % Determine if this is a number, vector or string
0054         n = str2num(token);
0055         if ~isempty(n),
0056             % It is a number, convert it to numerical format
0057             b.field{item,field} = n;
0058         elseif token(1) ~= '[',
0059             % It is a string, keep it as it is
0060             b.field{item,field} = token;
0061         else
0062             % It is a vector (or matrix)
0063             [token,line] = strtok([token line],'[]');
0064             if isempty(regexprep(token,'( |\t)','')),
0065                 % Empty matrix (square brackets separated by spaces or tabs)
0066                 b.field{item,field} = [];
0067             else
0068                 % Make sure this is a numeric matrix
0069                 n = str2num(token);
0070                 if isempty(n),
0071                     error(['Incorrect vector or matrix in batch file (line ' int2str(l) ', element ' int2str(field) ').']);
0072                 end
0073                 b.field{item,field} = n;
0074             end
0075             line = line(2:end);
0076         end
0077     end
0078     if field > 0,
0079         % Make sure all lines have the same number of elements
0080         if item == 1,
0081             nFields = field;
0082         elseif field ~= nFields,
0083             if l == 2,
0084                 error(['Incoherent numbers of elements in batch file (' int2str(nFields)  ' elements on line 1, but ' int2str(field) ' elements on line 2).']);
0085             else
0086                 error(['Incoherent numbers of elements in batch file (' int2str(nFields)  ' elements on lines 1-' int2str(l-1) ', but ' int2str(field) ' elements on line ' int2str(l) ').']);
0087             end
0088         end
0089         % Next item
0090         b.line(item) = l;
0091         item = item + 1;
0092     end
0093     % Update line counter (for error messages)
0094     l = l + 1;
0095 end
0096 
0097 % Close file
0098 fclose(f);
0099 
0100 % Reset iterator
0101 b.currentItem = 0;
0102 b.currentField = 0;
0103 
0104 % Empty fields
0105 b.log = -1;
0106 b.mfile = [];

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