From 60a8e504f88a71cfdc0c11c80f3191faf1989494 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 9 Oct 2024 17:52:20 +0200
Subject: [PATCH] write_table_short.m: factorization

Also do not duplicate stack_solve_algo values, rather pass them as an argument.
---
 matlab/run_all_simulations.m |   2 +-
 matlab/write_table_short.m   | 119 +++++++++--------------------------
 2 files changed, 31 insertions(+), 90 deletions(-)

diff --git a/matlab/run_all_simulations.m b/matlab/run_all_simulations.m
index 2f8c545..3175e41 100644
--- a/matlab/run_all_simulations.m
+++ b/matlab/run_all_simulations.m
@@ -53,7 +53,7 @@ run_simulations(MODEL, stack_solve_algo, static_solve_algo, niter, ccrit);
 
 texname = sprintf('%s-%s', MODEL, lower(regexprep(char(java.net.InetAddress.getLocalHost.getHostName),'.local\>',''))); % TODO: Check if we need a trap for Octave.
 
-write_table_short(sprintf('simulations-%s.mat', MODEL), texname, nopng)
+write_table_short(sprintf('simulations-%s.mat', MODEL), texname, nopng, stack_solve_algo)
 
 % Delete files and subfolders
 delete(sprintf('%s.log', MODEL))
diff --git a/matlab/write_table_short.m b/matlab/write_table_short.m
index 3dac9da..0d6d4ed 100644
--- a/matlab/write_table_short.m
+++ b/matlab/write_table_short.m
@@ -1,4 +1,4 @@
-function write_table_short(datafile, texfilename, nopng)
+function write_table_short(datafile, texfilename, nopng, stack_solve_algo)
 
 % Produce a table of results with LaTex compiled as a pdf file and converted to a png file.
 %
@@ -15,12 +15,9 @@ if nargin<3
     nopng = true;
 end
 
-stack_solve_algo = [0, 1, 2, 3, 4, 5];
-id = 1:length(stack_solve_algo);
-
 cfolder = pwd;
 
-load(datafile);
+D = load(datafile); % Must be in a local variable since we have a nested function below
 
 [folder,~, ~] = fileparts(mfilename('fullpath'));
 idf = strfind(folder, filesep());
@@ -29,82 +26,48 @@ fid = fopen(sprintf('%stables/template-short.tex', rootfolder),'r');
 T = fread(fid, '*char')';
 fclose(fid);
 
+function replace_template_field(field_name, value)
+    if ~isnan(value)
+        switch value
+            case -1
+                T = strrep(T, field_name, 'crash');  % code crash
+            case -2
+                T = strrep(T, field_name, 'FAIL');   % algorithm did not converge
+            case -3
+                T = strrep(T, field_name, 'CRASH');  % matlab crash
+            case -10
+                T = strrep(T, field_name, 'NA');  % not available
+            otherwise
+                T = strrep(T, field_name, int2str(value));
+        end
+    end
+end
+
 %
 % Matlab
 %
 for algo = stack_solve_algo
-    if ~isnan(MATLAB_computing(id(stack_solve_algo==algo), 1))
-        switch MATLAB_computing(id(stack_solve_algo==algo), 1)
-          case -1
-            T = strrep(T, sprintf('{a%u_mat}', algo), 'crash');  % code crash
-          case -2
-            T = strrep(T, sprintf('{a%u_mat}', algo), 'FAIL');   % algorithm did not converge
-          case -3
-            T = strrep(T, sprintf('{a%u_mat}', algo), 'CRASH');  % matlab crash
-          case -10
-            T = strrep(T, sprintf('{a%u_mat}', algo), 'NA');  % not available
-          otherwise
-            T = strrep(T, sprintf('{a%u_mat}', algo), int2str(MATLAB_computing(id(stack_solve_algo==algo), 1)));
-        end
-    end
+    replace_template_field(sprintf('{a%u_mat}', algo), D.MATLAB_computing(find(stack_solve_algo==algo), 1));
 end
 %
 % use_dll
 %
 for algo = stack_solve_algo
-    if ~isnan(USE_DLL_computing(id(stack_solve_algo==algo), 1))
-        switch USE_DLL_computing(id(stack_solve_algo==algo), 1)
-          case -1
-            T = strrep(T, sprintf('{a%u_dll}', algo), 'crash');
-          case -2
-            T = strrep(T, sprintf('{a%u_dll}', algo), 'FAIL');
-          case -3
-            T = strrep(T, sprintf('{a%u_dll}', algo), 'CRASH');
-          case -10
-            T = strrep(T, sprintf('{a%u_dll}', algo), 'NA');
-          otherwise
-            T = strrep(T, sprintf('{a%u_dll}', algo), int2str(USE_DLL_computing(id(stack_solve_algo==algo), 1)));
-        end
-    end
+    replace_template_field(sprintf('{a%u_dll}', algo), D.USE_DLL_computing(find(stack_solve_algo==algo), 1));
 end
 %
 % Bytecode
 %
 for algo = stack_solve_algo
-    if ~isnan(BYTECODE_computing(id(stack_solve_algo==algo), 1))
-        switch BYTECODE_computing(id(stack_solve_algo==algo), 1)
-          case -1
-            T = strrep(T, sprintf('{a%u_byt}', algo), 'crash');
-          case -2
-            T = strrep(T, sprintf('{a%u_byt}', algo), 'FAIL');
-          case -3
-            T = strrep(T, sprintf('{a%u_byt}', algo), 'CRASH');
-          case -10
-            T = strrep(T, sprintf('{a%u_byt}', algo), 'NA');
-          otherwise
-            T = strrep(T, sprintf('{a%u_byt}', algo), int2str(BYTECODE_computing(id(stack_solve_algo==algo), 1)));
-        end
-    end
+    replace_template_field(sprintf('{a%u_byt}', algo), D.BYTECODE_computing(find(stack_solve_algo==algo), 1));
 end
 %
 % Matlab with blocks
 %
 for mfs = 0:3
     for algo = stack_solve_algo
-        if ~isnan(MATLAB_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1))
-            switch MATLAB_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1)
-              case -1
-                T = strrep(T, sprintf('{a%u_mat_blk_%u}', algo, mfs), 'crash');
-              case -2
-                T = strrep(T, sprintf('{a%u_mat_blk_%u}', algo, mfs), 'FAIL');
-              case -3
-                T = strrep(T, sprintf('{a%u_mat_blk_%u}', algo, mfs), 'CRASH');
-              case -10
-                T = strrep(T, sprintf('{a%u_mat_blk_%u}', algo, mfs), 'NA');
-              otherwise
-                T = strrep(T, sprintf('{a%u_mat_blk_%u}', algo, mfs), int2str(MATLAB_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1)));
-            end
-        end
+        replace_template_field(sprintf('{a%u_mat_blk_%u}', algo, mfs), ...
+                               D.MATLAB_WITH_BLOCKS_computing(find(stack_solve_algo==algo), mfs+1));
     end
 end
 %
@@ -112,20 +75,8 @@ end
 %
 for mfs = 0:3
     for algo = stack_solve_algo
-        if ~isnan(USE_DLL_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1))
-            switch USE_DLL_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1)
-              case -1
-                T = strrep(T, sprintf('{a%u_dll_blk_%u}', algo, mfs), 'crash');
-              case -2
-                T = strrep(T, sprintf('{a%u_dll_blk_%u}', algo, mfs), 'FAIL');
-              case -3
-                T = strrep(T, sprintf('{a%u_dll_blk_%u}', algo, mfs), 'CRASH');
-              case -10
-                T = strrep(T, sprintf('{a%u_dll_blk_%u}', algo, mfs), 'NA');
-              otherwise
-                T = strrep(T, sprintf('{a%u_dll_blk_%u}', algo, mfs), int2str(USE_DLL_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1)));
-            end
-        end
+        replace_template_field(sprintf('{a%u_dll_blk_%u}', algo, mfs), ...
+                               D.USE_DLL_WITH_BLOCKS_computing(find(stack_solve_algo==algo), mfs+1));
     end
 end
 %
@@ -133,20 +84,8 @@ end
 %
 for mfs = 0:3
     for algo = stack_solve_algo
-        if ~isnan(BYTECODE_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1))
-            switch BYTECODE_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1)
-              case -1
-                T = strrep(T, sprintf('{a%u_byt_blk_%u}', algo, mfs), 'crash');
-              case -2
-                T = strrep(T, sprintf('{a%u_byt_blk_%u}', algo, mfs), 'FAIL');
-              case -3
-                T = strrep(T, sprintf('{a%u_byt_blk_%u}', algo, mfs), 'CRASH');
-              case -10
-                T = strrep(T, sprintf('{a%u_byt_blk_%u}', algo, mfs), 'NA');
-              otherwise
-                T = strrep(T, sprintf('{a%u_byt_blk_%u}', algo, mfs), int2str(BYTECODE_WITH_BLOCKS_computing(id(stack_solve_algo==algo), mfs+1)));
-            end
-        end
+        replace_template_field(sprintf('{a%u_byt_blk_%u}', algo, mfs), ...
+                               D.BYTECODE_WITH_BLOCKS_computing(find(stack_solve_algo==algo), mfs+1));
     end
 end
 
@@ -178,3 +117,5 @@ cd(sprintf('%stables', rootfolder));
 system(sprintf('rm %s.log', texfilename));
 system(sprintf('rm %s.aux', texfilename));
 cd(cfolder);
+
+end
-- 
GitLab