valid_img

PURPOSE ^

[pass, err_str] = valid_img(img)

SYNOPSIS ^

function pass = valid_img(img)

DESCRIPTION ^

 [pass, err_str] = valid_img(img)

 Confirms that a valid image structure exists or
 explain why an image 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_img(img)
0002 % [pass, err_str] = valid_img(img)
0003 %
0004 % Confirms that a valid image structure exists or
0005 % explain why an image 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_img.m 4986 2015-05-11 20:09:28Z aadler $
0013 
0014 pass = 1;
0015 err_str = '';
0016 
0017 % it's a struct with fields
0018 if ~isstruct(img)
0019    pass = 0;
0020    err_str = [err_str '- not a struct\n'];
0021 end
0022 
0023 % required fields
0024 %      field       type
0025 f = {'name',      'char', ...
0026      'elem_data', 'numeric', ...
0027      'fwd_model', 'struct', ...
0028      'type',      'char'};
0029 %     'inv_model', 'struct', ...
0030 for i=1:length(f)/2
0031    x=2*(i-1)+1;
0032    y=x+1;
0033    if ~isfield(img, f{x})
0034       pass = 0;
0035       err_str = [err_str '- missing required field: ' f{x} '\n'];
0036    elseif ~isa(img.(f{x}), f{y})
0037       pass = 0;
0038       err_str = [err_str '- required field ' f{x} ' is not a ' f{y}'\n'];
0039    end
0040 end
0041 % check the fwd_model
0042 [pass_local, err_str_local] = valid_fwd_model(img.fwd_model,'rec_model');
0043 if ~pass_local
0044    pass = 0;
0045    disp(err_str_local);
0046    err_str_local = strrep(err_str_local, ' "', ' "fwd_model.');
0047    err_str = [err_str err_str_local];
0048 end
0049 clear err_str_local pass_local;
0050 % check for correct 'type'
0051 if ~strcmp(img.type, 'image')
0052    pass = 0;
0053    err_str = [err_str '- field "type" must be "image"\n'];
0054 end
0055 
0056 % optional fields
0057 %      field       type
0058 f = {'inv_model', 'struct'};
0059 for i=1:length(f)/2
0060    x=2*(i-1)+1;
0061    y=x+1;
0062    if isfield(img, f{x}) && ~isa(img.(f{x}), f{y})
0063       pass = 0;
0064       err_str = [err_str '- optional field ' f{x} ' is not a ' f{y}'\n'];
0065    end
0066 end
0067 % check the inv_model
0068 if isfield(img, 'inv_model')
0069    [pass_local, err_str_local] = valid_inv_model(img.inv_model);
0070    if ~pass_local
0071       pass = 0;
0072       disp(err_str_local);
0073       err_str_local = strrep(err_str_local, ' "', ' "inv_model.');
0074       err_str = [err_str err_str_local];
0075    end
0076    clear err_str_local pass_local;
0077 end
0078 
0079 % illegal fields
0080 %      field       type
0081 f = {'imdl', 'inv_mdl', 'fmdl', 'fwd_mdl'};
0082 for i=1:length(f)
0083    x=i;
0084    if isfield(img, f{x})
0085       pass = 0;
0086       err_str = [err_str '- illegal field "' f{x} '" found\n'];
0087    end
0088 end
0089 
0090 % result
0091 if ~pass
0092    err_str = err_str(1:end-2); % drop last \n
0093 end
0094 if ( nargout == 0 ) && ~pass
0095    error(sprintf(['Reasons:\n' err_str]));
0096 end

Generated on Wed 21-Jun-2017 09:29:07 by m2html © 2005