0001 function opt = ng_read_opt(fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 if nargin == 1 && ischar(fname) && strcmp(fname,'UNIT_TEST')
0015 do_unit_test; return; end
0016
0017 if nargin == 0
0018 fname = 'ng.opt';
0019 end
0020
0021 if exist(fname,'dir')
0022 fname = [fname filesep 'ng.opt'];
0023 end
0024
0025 opt = struct();
0026
0027 fid = fopen(fname,'r');
0028 tline = fgetl(fid);
0029 while ischar(tline)
0030 [key, val] = strtok(tline,' ');
0031
0032 strval= regexp(val,'\s*(.*\S)\s*','tokens');
0033 if isempty(strval)
0034 strval = '';
0035 else
0036 strval= strval{1}{1};
0037 end
0038 numval = str2double(strval);
0039 if isnan(numval)
0040 eval(sprintf('opt.%s = ''%s'';',key,strval));
0041 else
0042 eval(sprintf('opt.%s = %f;',key,numval));
0043 end
0044 tline = fgetl(fid);
0045 end
0046 fclose(fid);
0047
0048
0049
0050 function do_unit_test
0051 opt = ng_write_opt();
0052 opt.meshoptions.fineness = 4;
0053 ng_write_opt(opt);
0054 opt = ng_read_opt();
0055 unit_test_cmp('simple test',opt.meshoptions.fineness, 4);
0056
0057 path = cd();
0058 opt = ng_read_opt(path);
0059 unit_test_cmp('dir test',opt.meshoptions.fineness, 4);
0060
0061 path = cd();
0062 opt = ng_read_opt([path filesep 'ng.opt']);
0063 unit_test_cmp('path test',opt.meshoptions.fineness, 4);
0064
0065
0066