valid_data

PURPOSE ^

[pass, err_str] = valid_data(data)

SYNOPSIS ^

function pass = valid_data(data)

DESCRIPTION ^

 [pass, err_str] = valid_data(data)

 Confirms that a valid measurement data structure exists or
 explain why an structure is not valid.

 If called without an output argument (argout=0), will
 error out if invalid. Otherwise the function is silent,
 returning an explaination of failures in err_str.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function pass = valid_data(data)
0002 % [pass, err_str] = valid_data(data)
0003 %
0004 % Confirms that a valid measurement data structure exists or
0005 % explain why an structure is not valid.
0006 %
0007 % If called without an output argument (argout=0), will
0008 % error out if invalid. Otherwise the function is silent,
0009 % returning an explaination of failures in err_str.
0010 
0011 % (C) 2015 Alistair Boyle. License: GPL version 2 or version 3
0012 % $Id: valid_data.m 4986 2015-05-11 20:09:28Z aadler $
0013 
0014 pass = 1;
0015 err_str = '';
0016 
0017 % it *can* be just straight up numbers
0018 if isnumeric(data)
0019    return;
0020 end
0021 
0022 % it's a struct with fields
0023 if ~isstruct(data)
0024    pass = 0;
0025    err_str = [err_str '- not a struct or numeric data\n'];
0026 end
0027 
0028 % required fields
0029 %      field       type
0030 f = {'name',      'char', ...
0031      'meas',      'numeric', ...
0032      'time',      'numeric', ... % OPTIONAL?
0033      'type',      'char'};
0034 for i=1:length(f)/2
0035    x=2*(i-1)+1;
0036    y=x+1;
0037    if ~isfield(data, f{x})
0038       pass = 0;
0039       err_str = [err_str '- missing required field: ' f{x} '\n'];
0040    elseif ~isa(data.(f{x}), f{y})
0041       pass = 0;
0042       err_str = [err_str '- required field ' f{x} ' is not a ' f{y}'\n'];
0043    end
0044 end
0045 % check for correct 'type'
0046 if ~strcmp(data.type, 'data')
0047    pass = 0;
0048    err_str = [err_str '- field "type" must be "data"\n'];
0049 end
0050 
0051 % optional fields
0052 %      field       type
0053 f = {};
0054 for i=1:length(f)/2
0055    x=2*(i-1)+1;
0056    y=x+1;
0057    if isfield(data, f{x}) && ~isa(data.(f{x}), f{y})
0058       pass = 0;
0059       err_str = [err_str '- optional field ' f{x} ' is not a ' f{y}'\n'];
0060    end
0061 end
0062 
0063 % illegal fields
0064 %      field
0065 f = {'maes'};
0066 for i=1:length(f)
0067    x=i;
0068    if isfield(data, f{x})
0069       pass = 0;
0070       err_str = [err_str '- illegal field "' f{x} '" found\n'];
0071    end
0072 end
0073 
0074 % result
0075 if ~pass
0076    err_str = err_str(1:end-2); % drop last \n
0077 end
0078 if ( nargout == 0 ) && ~pass
0079    error(sprintf(['Reasons:\n' err_str]));
0080 end

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005