From d560e59a332b06b87408cebcd01d4d7d6793060b Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 29 Jul 2014 12:34:20 +0200
Subject: [PATCH] reporting: add writeCSV option for graphs, #693

---
 @graph/graph.m          |  4 +++-
 @graph/writeGraphFile.m | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/@graph/graph.m b/@graph/graph.m
index 7aa0525..4d73da5 100644
--- a/@graph/graph.m
+++ b/@graph/graph.m
@@ -85,6 +85,8 @@ o.height = 4.5;
 o.miscTikzPictureOptions = '';
 o.miscTikzAxisOptions = '';
 
+o.writeCSV = false;
+
 if nargin == 1
     assert(isa(varargin{1}, 'graph'),['@graph.graph: with one arg you ' ...
                         'must pass a graph object']);
@@ -139,7 +141,7 @@ assert(isint(o.yTickLabelPrecision), '@graph.graph: o.yTickLabelPrecision must b
 assert(islogical(o.yTickLabelFixed), '@graph.graph: yTickLabelFixed must be either true or false');
 assert(islogical(o.yTickLabelZeroFill), '@graph.graph: yTickLabelZeroFill must be either true or false');
 assert(islogical(o.yTickLabelScaled), '@graph.graph: yTickLabelScaled must be either true or false');
-
+assert(islogical(o.writeCSV), '@graph.graph: writeCSV must be either true or false');
 assert(ischar(o.shadeColor), '@graph.graph: shadeColor must be a string');
 assert(ischar(o.zeroLineColor), '@graph.graph: zeroLineColor must be a string');
 assert(any(strcmp(o.axisShape, {'box', 'L'})), ['@graph.graph: axisShape ' ...
diff --git a/@graph/writeGraphFile.m b/@graph/writeGraphFile.m
index bd820c9..85b09d4 100644
--- a/@graph/writeGraphFile.m
+++ b/@graph/writeGraphFile.m
@@ -231,8 +231,25 @@ if o.showZeroline
         o.zeroLineColor, dd.ndat);
 end
 
+if o.writeCSV
+    csvseries = dseries();
+end
 for i=1:ne
     o.series{i}.writeSeriesForGraph(fid, dd);
+    if o.writeCSV
+        csvseries = [csvseries ...
+            o.series{i}.data(dd).set_names([...
+            o.series{i}.data.name{:} '_' ...
+            o.series{i}.graphLegendName '_' ...
+            o.series{i}.graphLineColor '_' ...
+            o.series{i}.graphLineStyle '_' ...
+            num2str(o.series{i}.graphLineWidth) '_' ...
+            o.series{i}.graphMarker '_' ...
+            o.series{i}.graphMarkerEdgeColor '_' ...
+            o.series{i}.graphMarkerFaceColor '_' ...
+            num2str(o.series{i}.graphMarkerSize)]) ...
+            ];
+    end
     if o.showLegend
         le = o.series{i}.getNameForLegend();
         if ~isempty(le)
@@ -240,6 +257,9 @@ for i=1:ne
         end
     end
 end
+if o.writeCSV
+    csvseries.save(strrep(o.graphName, '.tex', ''), 'csv');
+end
 fprintf(fid, '\\end{axis}\n\\end{tikzpicture}%%');
 if fclose(fid) == -1
     error('@graph.writeGraphFile: closing %s\n', o.filename);
-- 
GitLab