diff --git a/ModFile.cc b/ModFile.cc
index 7e73f550e89bb2841fb9540d3eb9f8514b433709..f5c20837c02aeec7c698ffdcf52f366b4173dac9 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -585,6 +585,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
               << "M_.Correlation_matrix = eye(" << symbol_table.exo_nbr() << ", "
               << symbol_table.exo_nbr() << ");" << endl;
 
+  // Initialize M_.det_shocks
+  mOutputFile << "M_.det_shocks = [];" << endl;
+
   if (mod_file_struct.calibrated_measurement_errors)
     mOutputFile << "M_.H = zeros(" << symbol_table.observedVariablesNbr() << ", "
                 << symbol_table.observedVariablesNbr() << ");" << endl
diff --git a/Shocks.cc b/Shocks.cc
index fef8bda4539e56951efea95875fc7a9317ef9014..25adbdee9a1d182c006c50799b4828f25f74ac9d 100644
--- a/Shocks.cc
+++ b/Shocks.cc
@@ -36,7 +36,6 @@ void
 AbstractShocksStatement::writeDetShocks(ostream &output) const
 {
   int exo_det_length = 0;
-  int counter = 1;
 
   for (det_shocks_t::const_iterator it = det_shocks.begin();
        it != det_shocks.end(); it++)
@@ -50,19 +49,14 @@ AbstractShocksStatement::writeDetShocks(ostream &output) const
           const int &period2 = it->second[i].period2;
           const expr_t value = it->second[i].value;
 
-          output << "M_.det_shocks(" << counter << ").exo_det=" << (int) exo_det
-                 << ";" << endl
-                 << "M_.det_shocks(" << counter << ").exo_id=" << id
-                 << ";" << endl
-                 << "M_.det_shocks(" << counter << ").multiplicative=" << (int) mshocks
-                 << ";" << endl
-                 << "M_.det_shocks(" << counter << ").periods=" << period1
-                 << ":" << period2 << ";" << endl
-                 << "M_.det_shocks(" << counter << ").value=(";
+          output << "M_.det_shocks = [ M_.det_shocks;" << endl
+                 << "struct('exo_det'," << (int) exo_det
+                 << ",'exo_id'," << id
+                 << ",'multiplicative'," << (int) mshocks
+                 << ",'periods'," << period1 << ":" << period2
+                 << ",'value',";
           value->writeOutput(output);
-          output << ");" << endl;
-
-          counter++;
+          output << ") ];" << endl;
 
           if (exo_det && (period2 > exo_det_length))
             exo_det_length = period2;