0001 function out = mdl_normalize(mdl, val)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 if ischar(mdl) && strcmp(mdl, 'UNIT_TEST'); do_unit_test; return; end
0021 if ischar(mdl) && strcmp(mdl, 'default')
0022 out = eidors_default;
0023 return
0024 end
0025
0026
0027 try
0028 switch mdl.type
0029 case 'fwd_model'
0030
0031 case 'image'
0032 img = mdl;
0033 mdl = img.fwd_model;
0034 otherwise
0035 error('EIDORS:WrongInput','Expected fwd_model or image.');
0036 end
0037 catch
0038 warning('EIDORS:UnknownType','Object has no type. Assuming fwd_model.');
0039 end
0040
0041 switch nargin
0042 case 1
0043 out = get_flag(mdl);
0044 if isempty(out)
0045 out = eidors_default;
0046 warning('EIDORS:normalize_flag_not_set', ...
0047 'normalize_measurements flag not set on model. Using default: %d',out);
0048 end
0049 case 2
0050 if ischar(val) && strcmp(val, 'default')
0051 val = eidors_default;
0052 end
0053 out = set_flag(mdl,val);
0054 if exist('img','var')
0055 img.fwd_model = out;
0056 out = img;
0057 end
0058 otherwise
0059 error('Wrong number of parameters');
0060 end
0061
0062
0063
0064 function out = get_flag(mdl)
0065 out = [];
0066
0067 ff = fieldnames(mdl);
0068 for i = 1:length(ff);
0069
0070 if any(strcmp(ff{i},{'normalize','normalize_measurements'}))
0071 out = mdl.(ff{i});
0072 else
0073
0074 token = regexp(ff{i},'normal[^s]+.*','match','once');
0075 if ~isempty(token);
0076 warning('Suspect field "%s" found on the model',token);
0077 end
0078
0079 end
0080 end
0081
0082 function mdl = set_flag(mdl, val)
0083 mdl = eidors_obj('set',mdl, 'normalize_measurements', val);
0084
0085 function do_unit_test
0086 s = warning('query', 'backtrace');
0087 warning off backtrace;
0088 eidors_default('set','mdl_normalize',@(x) 0);
0089 mdl = mk_common_model('c2t2',16);
0090 fmdl = mdl.fwd_model;
0091 unit_test_cmp('Does it come normalized?', mdl_normalize(fmdl), 0);
0092 fmdl = mdl_normalize(fmdl,1);
0093 unit_test_cmp('Normalize it. How about now?', mdl_normalize(fmdl), 1);
0094 fmdl = mdl_normalize(fmdl,0);
0095 unit_test_cmp('De-normalize it. And now?', mdl_normalize(fmdl), 0);
0096 fmdl = rmfield(fmdl,'normalize_measurements');
0097 eidors_msg(mdl_normalize(fmdl));
0098 unit_test_cmp('Remove the flag', ...
0099 lastwarn, 'normalize_measurements flag not set on model. Using default: 0');
0100 fmdl.normalise_measurements = 1;
0101 eidors_msg(mdl_normalize(fmdl));
0102 unit_test_cmp('Spelling error', ...
0103 lastwarn, 'normalize_measurements flag not set on model. Using default: 0');
0104 fmdl.normalize_my_gran = 1;
0105 eidors_msg(mdl_normalize(fmdl));
0106 unit_test_cmp('And now let''s set it to something stupid', ...
0107 lastwarn, 'normalize_measurements flag not set on model. Using default: 0');
0108 fmdl.normalize = 1;
0109 unit_test_cmp('Accept happily', mdl_normalize(fmdl), 1);
0110 eidors_msg('But an imdl is not acceptable!');
0111 try
0112 eidors_msg(mdl_normalize(mdl));
0113 catch
0114 unit_test_cmp('Just so.', 1,1);
0115 end
0116 warning(s);