From 47af0cde165155aaff1f2f98106e77315330d48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Guts=29?= Date: Fri, 15 Dec 2023 16:33:52 +0100 Subject: [PATCH] Remove calls to eval. --- matlab/MakeAllFigures.m | 32 +++++++++++-------- .../convergence_diagnostics/geweke_moments.m | 4 +-- .../display_estimation_results_table.m | 22 ++++++------- matlab/estimation/model_comparison.m | 2 +- matlab/get_new_or_existing_ei_index.m | 12 +++---- matlab/parallel/storeGlobalVars.m | 6 ++-- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/matlab/MakeAllFigures.m b/matlab/MakeAllFigures.m index e14f86579..92b7a3f2c 100644 --- a/matlab/MakeAllFigures.m +++ b/matlab/MakeAllFigures.m @@ -56,7 +56,8 @@ elseif NumberOfPlots == 1 end for plt = 1:NumberOfPlots - eval(['NumberOfCurves = Info.Box' int2str(plt) '.Number;']) + field1 = sprintf('Box%u', plt); + NumberOfCurves = Info.(field1).Number; NumberOfObservations = zeros(2,1); x = cell(NumberOfCurves,1); y = cell(NumberOfCurves,1); @@ -66,10 +67,11 @@ for plt = 1:NumberOfPlots binf = NaN(NumberOfCurves,1); bsup = NaN(NumberOfCurves,1); for curve = 1:NumberOfCurves - eval(['x{' curve '} = Info.Box' int2str(plt) '.Curve' int2str(curve) '.xdata;']) - eval(['y{' curve '} = Info.Box' int2str(plt) '.Curve' int2str(curve) '.ydata;']) - eval(['name = Info.Box' int2str(plt) '.Curve' int2str(curve) '.variablename;']) - eval(['PltType{' curve '} = Info.Box' int2str(plt) '.Curve' int2str(curve) '.type']); + field2 = sprintf('Curve%u', curve); + x{curve} = Info.(field1).(field2).xdata; + y{curve} = Info.(field1).(field2).ydata; + name = Info.(field1).(field2).variablename; + PltType{curve} = Info.(field1).(field2).type; if length(x{curve})-length(y{curve}) disp('MakeFigure :: The number of observations in x doesn''t match with ') disp(['the number of observation in y for ' name ]) @@ -90,9 +92,9 @@ for plt = 1:NumberOfPlots if isnan(ymin(plt)) ymin = 0; end - eval(['NAMES{' int2str(plt) '} = Info.Box' int2str(plt) '.name;']) + NAMES{plt} = Info.(field1).name; if options_.TeX - eval(['TeXNAMES{' int2str(plt) '} = Info.Box' int2str(plt) '.texname;']) + TeXNAMES{plt} = Info.(field1).texname; end subplot(nr,nc,plt) hold on @@ -148,22 +150,24 @@ end if Info.SaveFormat.Eps if isempty(Info.SaveFormat.Name) - eval(['print -depsc2 ' M_.fname Info.SaveFormat.GenericName int2str(Info.SaveFormat.Number) '.eps']); + print(sprintf('%s%s%u', M_.fname, Info.SaveFormat.GenericName, Info.SaveFormat.Number), '-depsc2') else - eval(['print -depsc2 ' M_.fname Info.SaveFormat.GenericName Info.SaveFormat.Name '.eps']); + print(sprintf('%s%s%s', M_.fname, Info.SaveFormat.GenericName, Info.SaveFormat.Name), '-depsc2') end end + if Info.SaveFormat.Pdf && ~isoctave if isempty(Info.SaveFormat.Name) - eval(['print -dpdf ' M_.fname Info.SaveFormat.GenericName int2str(Info.SaveFormat.Number)]); + print(sprintf('%s%s%u', M_.fname, Info.SaveFormat.GenericName, Info.SaveFormat.Number), '-dpdf') else - eval(['print -dpdf ' M_.fname Info.SaveFormat.GenericName Info.SaveFormat.Name]); + print(sprintf('%s%s%s', M_.fname, Info.SaveFormat.GenericName, Info.SaveFormat.Name), '-dpdf') end end + if Info.SaveFormat.Fig && ~isoctave if isempty(Info.SaveFormat.Name) - saveas(FigHandle,[M_.fname Info.SaveFormat.GenericName int2str(Info.SaveFormat.Number) '.fig']); + saveas(FigHandle, sprintf('%s%s%u.fig', M_.fname, Info.SaveFormat.GenericName, Info.SaveFormat.Number)); else - saveas(FigHandle,[M_.fname Info.SaveFormat.GenericName Info.SaveFormat.Name '.fig']); + saveas(FigHandle, sprintf('%s%s%s.fig', M_.fname, Info.SaveFormat.GenericName, Info.SaveFormat.Name)); end -end \ No newline at end of file +end diff --git a/matlab/convergence_diagnostics/geweke_moments.m b/matlab/convergence_diagnostics/geweke_moments.m index e6736132c..dd713b82a 100644 --- a/matlab/convergence_diagnostics/geweke_moments.m +++ b/matlab/convergence_diagnostics/geweke_moments.m @@ -103,6 +103,6 @@ for taper_index=1:length(taper_steps) results_vec(1,4+taper_index*2-1)=NSE_taper; results_vec(1,4+taper_index*2)=total_variance/(n_draws_used*NSE_taper^2); - eval(['results_struct.nse_taper_',num2str(taper),'= NSE_taper;']); - eval(['results_struct.rne_taper_',num2str(taper),'= total_variance/(n_draws_used*NSE_taper^2);']); + results_struct.(sprintf('nse_taper_%u', taper)) = NSE_taper; + results_struct.(sprintf('rne_taper_%u', taper)) = total_variance/(n_draws_used*NSE_taper^2); end % end of for mm loop diff --git a/matlab/estimation/display_estimation_results_table.m b/matlab/estimation/display_estimation_results_table.m index 6078d5031..09f7b794a 100644 --- a/matlab/estimation/display_estimation_results_table.m +++ b/matlab/estimation/display_estimation_results_table.m @@ -72,8 +72,8 @@ if np fprintf('%-*s %10.4f %7.4f %7.4f \n', ... header_width, name, xparam1(ip), stdh(ip), tstath(ip)); end - eval(['oo_.' field_name '_mode.parameters.' name ' = xparam1(ip);']); - eval(['oo_.' field_name '_std_at_mode.parameters.' name ' = stdh(ip);']); + oo_.(sprintf('%s_mode', field_name)).parameters.(name) = xparam1(ip); + oo_.(sprintf('%s_std_at_mode', field_name)).parameters.(name) = stdh(ip); ip = ip+1; end skipline() @@ -94,8 +94,8 @@ if nvx fprintf('%-*s %10.4f %7.4f %7.4f \n', header_width, name, xparam1(ip), stdh(ip), tstath(ip)); end M_.Sigma_e(k,k) = xparam1(ip)*xparam1(ip); - eval(['oo_.' field_name '_mode.shocks_std.' name ' = xparam1(ip);']); - eval(['oo_.' field_name '_std_at_mode.shocks_std.' name ' = stdh(ip);']); + oo_.(sprintf('%s_mode', field_name)).shocks_std.(name) = xparam1(ip); + oo_.(sprintf('%s_std_at_mode', field_name)).shocks_std.(name) = stdh(ip); ip = ip+1; end skipline() @@ -116,8 +116,8 @@ if nvn fprintf('%-*s %10.4f %7.4f %7.4f \n', header_width, name, xparam1(ip), ... stdh(ip), tstath(ip)) end - eval(['oo_.' field_name '_mode.measurement_errors_std.' name ' = xparam1(ip);']); - eval(['oo_.' field_name '_std_at_mode.measurement_errors_std.' name ' = stdh(ip);']); + oo_.(sprintf('%s_mode', field_name)).measurement_errors_std.(name) = xparam1(ip); + oo_.(sprintf('%s_std_at_mode', field_name)).measurement_errors_std.(name) = stdh(ip); ip = ip+1; end skipline() @@ -142,8 +142,8 @@ if ncx end M_.Sigma_e(k1,k2) = xparam1(ip)*sqrt(M_.Sigma_e(k1,k1)*M_.Sigma_e(k2,k2)); M_.Sigma_e(k2,k1) = M_.Sigma_e(k1,k2); - eval(['oo_.' field_name '_mode.shocks_corr.' NAME ' = xparam1(ip);']); - eval(['oo_.' field_name '_std_at_mode.shocks_corr.' NAME ' = stdh(ip);']); + oo_.(sprintf('%s_mode', field_name)).shocks_corr.(name) = xparam1(ip); + oo_.(sprintf('%s_std_at_mode', field_name)).shocks_corr.(name) = stdh(ip); ip = ip+1; end skipline() @@ -166,8 +166,8 @@ if ncn fprintf('%-*s %10.4f %7.4f %7.4f \n',header_width, name, xparam1(ip), ... stdh(ip), tstath(ip)); end - eval(['oo_.' field_name '_mode.measurement_errors_corr.' NAME ' = xparam1(ip);']); - eval(['oo_.' field_name '_std_at_mode.measurement_errors_corr.' NAME ' = stdh(ip);']); + oo_.(sprintf('%s_mode', field_name)).measurement_errors_corr.(name) = xparam1(ip); + oo_.(sprintf('%s_std_at_mode', field_name)).measurement_errors_corr.(name) = stdh(ip); ip = ip+1; end skipline() @@ -407,4 +407,4 @@ function TeXEnd(fid) fprintf(fid,'\\end{longtable}\n '); fprintf(fid,'\\end{center}\n'); fprintf(fid,'%% End of TeX file.\n'); -fclose(fid); \ No newline at end of file +fclose(fid); diff --git a/matlab/estimation/model_comparison.m b/matlab/estimation/model_comparison.m index 88367b5ac..ac27031d3 100644 --- a/matlab/estimation/model_comparison.m +++ b/matlab/estimation/model_comparison.m @@ -87,7 +87,7 @@ for i=1:NumberOfModels end end try - eval(['MarginalLogDensity(i) = mstruct.oo_.MarginalDensity.' type ';']) + MarginalLogDensity(i) = mstruct.oo_.MarginalDensity.(type); catch if strcmpi(type,'LaplaceApproximation') if isfield(mstruct.oo_,'mle_mode') diff --git a/matlab/get_new_or_existing_ei_index.m b/matlab/get_new_or_existing_ei_index.m index d456ea51e..e179e6748 100644 --- a/matlab/get_new_or_existing_ei_index.m +++ b/matlab/get_new_or_existing_ei_index.m @@ -36,24 +36,24 @@ function indx = get_new_or_existing_ei_index(substructure_name, name1, name2) global estimation_info -if eval(['isempty(estimation_info.' substructure_name ')']) +if isempty(estimation_info.(substructure_name)) indx = 1; return end if isempty(name2) % parameter or std() statement - indx = eval(['find(strcmp(name1, estimation_info.' substructure_name ') == 1)']); + indx = find(strcmp(name1, estimation_info.(substructure_name))); else % corr statement - indx = eval(['find(strcmp([''' name1 ':' name2 '''], estimation_info.' substructure_name ') == 1)']); + indx = find(strcmp(sprintf('%s:%s', name1, name2), estimation_info.(substructure_name))); if isempty(indx) - indx = eval(['find(strcmp([''' name2 ':' name1 '''], estimation_info.' substructure_name ') == 1)']); + indx = find(strcmp(sprintf('%s:%s', name2, name1), estimation_info.(substructure_name))); end end if isempty(indx) - indx = eval(['size(estimation_info.' substructure_name ', 2) + 1']); + indx = size(estimation_info.(substructure_name), 2) + 1; end if size(indx, 2) > 1 - error(['Error: ' name1 ' ' name2 'found more than once in estimation_info.subsamples_index']); + error('Error: %s %s found more than once in estimation_info.subsamples_index', name1, name2); end diff --git a/matlab/parallel/storeGlobalVars.m b/matlab/parallel/storeGlobalVars.m index eade9fc3c..8ab05dc1d 100644 --- a/matlab/parallel/storeGlobalVars.m +++ b/matlab/parallel/storeGlobalVars.m @@ -31,14 +31,14 @@ function storeGlobalVars(fname,append) GlobalNames = who('global'); -% varlist = ''; + for j=1:length(GlobalNames) eval(['global ',GlobalNames{j},';']); - eval(['fGlobalVar.',GlobalNames{j},'=',GlobalNames{j},';']); + fGlobalvar.(GlobalNames{j}) = GlobalNames{j}; end if nargin<2 save(fname,'fGlobalVar'); else save(fname,'fGlobalVar','-append'); -end \ No newline at end of file +end