From bd3d59dfe7960bfc50b5dad93db73ae28f41ff34 Mon Sep 17 00:00:00 2001
From: Jan Lemmer <jan.lemmer@fst.tu-darmstadt.de>
Date: Tue, 24 Aug 2021 09:09:33 +0200
Subject: [PATCH] Fixes #21, Fixes Error when trying to export the same ID
 again

---
 CI_files/default_test.m |  6 ++++++
 example.m               | 44 ++++++++++++++++++++++++++++++-----------
 fcn_core/Publish.m      | 41 +++++++++++++++++++++++---------------
 fcn_core/TagPlot.m      | 24 ++++++++++++++--------
 4 files changed, 80 insertions(+), 35 deletions(-)

diff --git a/CI_files/default_test.m b/CI_files/default_test.m
index 515ad37..d346c13 100644
--- a/CI_files/default_test.m
+++ b/CI_files/default_test.m
@@ -5,6 +5,12 @@ function [result] = default_test()
 clear; clc; close all;
 addpath('./fcn_core','./fcn_help');
 
+% clean up, if previous run failed
+try
+    delete CI_files/export/* CI_files/*.mat CI_files/*.h5
+    rmdir('CI_files/export','s');
+end
+
 ProjectID = 'Test01';
 %% Data
 % some random data
diff --git a/example.m b/example.m
index 6e03db9..16f69b5 100644
--- a/example.m
+++ b/example.m
@@ -55,33 +55,55 @@ a = example_fcn(a);
 % Make sure to save each figure in a variable to pass to PlotID-functions.
 fig(1) = figure;
 plot(x,y,'-k');
-box off
-set(gca, 'TickDir', 'out', 'YLim', [0,4]);
-
-hold on
-%fig(2) = figure;
+box off; hold on;
 plot(x1,y1,'-r');
 set(gca, 'TickDir', 'out', 'YLim', [0,4]);
 
+%% Example 1: single plot based on two data-sets
+
 %% Tag the plot
 % PlotID Implementation starts here.
-% TagPlot adds a visible ID to the figure and to the property fig.Tag
-[figs, ID] = TagPlot(fig, ProjectID);
-
+% TagPlot adds a visible ID to the figure(s) and to the figures property
+% 'Tag'
+[fig, ID] = TagPlot(fig, ProjectID);
 
 %% Publishing
 % Second part of plotID 
 % The functions needs the file location, the location of the data and the
 % figure and can take several options.
 % TODO add explanations for Options
-path.script = mfilename('fullpath'); % filename of the m.script
 
-% file name of the dataset
+path.script = mfilename('fullpath'); % filename of the m.script
+% file names of the datasets
 path.rdata =  {dataset1,dataset2} ; % don't forget the extension
 
-Publish(path, ID, figs, 'Location', 'local','Method','centraliced')
+Publish(path, ID, fig(1), 'Location', 'local','Method','centraliced')
+
+%% Example 2 : multiple plots plot, all based on two data-set2
+% for individual data-sets, use an appropriate array
+
+fig(2) = figure;
+plot(x,y,'-b');
+box off; hold on;
+plot(x1,y1,'--k');
+set(gca, 'TickDir', 'out', 'YLim', [0,4]);
+
+% tag both plots 
+[fig, IDs] = TagPlot(fig, ProjectID);
+
+% data locations
+path.script = mfilename('fullpath'); % filename of the m.script
+% file names of the datasets
+path.rdata =  {dataset2} ; % don't forget the extension
+
+% publsihing via for-loop
+for i=1: numel(fig)
+    disp(string(i));
+    Publish(path, IDs{i}, fig(i), 'Location', 'local','Method','centraliced');
+end
 
 %% Second Plot with identical data to test centralized method
+% ToDO Add better description
 
 fig2 =figure;
 plot(x,y,'-k');
diff --git a/fcn_core/Publish.m b/fcn_core/Publish.m
index cc81290..3c21b56 100644
--- a/fcn_core/Publish.m
+++ b/fcn_core/Publish.m
@@ -1,4 +1,4 @@
-function Publish(DataPaths, ID, figures, options)
+function Publish(DataPaths, ID, figure, options)
 %Publishes saves plot, data and measuring script 
 %   Location sets the storage location. 'local' sets the storage location
 %   to the current folder (an export folder will be created), 'server' is a
@@ -10,12 +10,21 @@ function Publish(DataPaths, ID, figures, options)
 arguments
    DataPaths
    ID (1,:) {mustBeNonzeroLengthText} % ID must be provided
-   figures (1,:) {mustBeFigure} % Checks if Figures are figures
+   figure (1,:) {mustBeFigure} % Checks if figure is a figure object
    options.Location {mustBeMember(options.Location ,['local','server','CI-Test'])} = 'local' % storage path
    options.Method {mustBeMember(options.Method ,['individual','centraliced'])} = 'individual'
    options.CopyUserFCN (1,1) {mustBeNumericOrLogical} = true 
 end
 
+%catch multiple figures in fig
+if numel(figure) > 1
+    figure = figure(1);
+    msg = ['Publish is designed for handeling one figure at once' newline,...
+        '- publishes uses only the first figure.' newline , ...
+        'Consider an external for loop for multi figure export as provided in example.m'];
+    warning(msg);
+end
+
 switch options.Location 
     case 'local'
          storPath = fullfile(pwd,'export');
@@ -28,7 +37,9 @@ folderName = char(ID);
 
 %% Create Data-Directory
 addpath(storPath);
-if mkdir(fullfile(storPath,folderName))
+if isfolder(fullfile(storPath,folderName))
+   error(['Folder ',folderName, ' exists - Plot was already published ']);
+elseif mkdir(fullfile(storPath,folderName))
 else
     error('Directory could not be created - check remote path and permissions');
 end
@@ -79,20 +90,18 @@ switch options.Method
         % Create a copy of the research data    
         createFileCopy(DataPaths.rdata,folderName,storPath,ID, 'data');
 end
-%% Export the Plots
-try 
-   for i=1:numel(figures)
-       fig = figures(i);
-       PlotName = [ID,'_plot',num2str(i)]; % Der Plotnamen
+%% Export the Plot
+%try 
+       PlotName = [ID,'_plot']; % plotname
        RemotePath = fullfile(storPath ,folderName, PlotName);
        % Matlab figure
-       savefig(fig,RemotePath);
-       exportgraphics(fig,[RemotePath,'.png'],'Resolution',300);
-       disp([num2str(i),' of ',num2str(numel(figures)),' figures exported']);
-   end
-catch 
-    warning('Plot export was not successful')
-end
+       savefig(figure,RemotePath);
+       % the png should only be a preview
+       exportgraphics(figure,[RemotePath,'.png'],'Resolution',300);
+%   end
+% catch 
+%     warning('Plot export was not successful')
+% end
 
 disp(['publishing of ', ID , ' done']);
 
@@ -100,5 +109,5 @@ end %function
 
 function tf = mustBeFigure(h)
 %checks if input is a figure object
-  tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');
+  tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');  
 end
diff --git a/fcn_core/TagPlot.m b/fcn_core/TagPlot.m
index b64d3d4..b3f0ae4 100644
--- a/fcn_core/TagPlot.m
+++ b/fcn_core/TagPlot.m
@@ -1,7 +1,9 @@
-function [figs, ID] = TagPlot(figs, prefix, options)
+function [figs, IDs] = TagPlot(figs, prefix, options)
 %TagPlot adds IDs to figures
 %   The ID is placed visual on the figure window and as Tag (property of figure)
-%   TagPlot can tag multiple figures at once
+%   TagPlot can tag multiple figures at once.
+%   If a single Plot is taged IDs is a char, otherwise it is a cell array of
+%   chars 
 %   prefix is the project number (string or char)
 %   
 %   The ID is placed on the 'east' per default, if you want it somwhere
@@ -44,21 +46,27 @@ switch options.Location
         warning([options.Location, ' is not a defined location, TagPlot uses location east instead']);
         options.Location = 'east'; options.Position = [1,0.4];
 end
-    
+
+IDs = cell(numel(figs),1);
+
 for n = 1:numel(figs)
-    ID = CreateID; % Create ID
-    ID = [prefix,'-',ID]; % add Prefix
+    IDs{n} = CreateID; % Create ID
+    IDs{n} = [prefix,'-',IDs{n}]; % add Prefix
     axes = get(figs(n),'CurrentAxes'); % Axes object for text annotation
     % Limits for relative Positioning
     ylim =get(axes,'YLim');
     xlim =get(axes,'XLim');
     %ID
     position = [options.Position(1)*xlim(2), options.Position(2)*ylim(2)];
-    text(axes,position(1),position(2), ID,'Fontsize',options.Fontsize,...
+    text(axes,position(1),position(2), IDs{n},'Fontsize',options.Fontsize,...
     'Rotation',options.Rotation, 'VerticalAlignment','bottom','Color',...
         0.65*[1 1 1],'BackgroundColor','w');
-    set(figs(n),'Tag', ID);
-    
+    set(figs(n),'Tag', IDs{n});    
+end
+
+if numel(figs) == 1
+   % use char instead of a cell arry for a single ID
+   IDs = IDs{1}; 
 end
 
 end
-- 
GitLab