Publish

PURPOSE ^

Publishes Saves Plot, Data and Measuring script

SYNOPSIS ^

function Publish(DataPaths, ID, figures, options)

DESCRIPTION ^

Publishes Saves Plot, Data and Measuring script 
   Detailed explanation goes here
   Location sets the storage location. local is in the current folder (an
   export folder will be created), server is a remote Path
   two Methods are implemented individual stores the data for each plot
   while centralized uses a data folder and uses reference links to the
   original data

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function Publish(DataPaths, ID, figures, options)
0002 %Publishes Saves Plot, Data and Measuring script
0003 %   Detailed explanation goes here
0004 %   Location sets the storage location. local is in the current folder (an
0005 %   export folder will be created), server is a remote Path
0006 %   two Methods are implemented individual stores the data for each plot
0007 %   while centralized uses a data folder and uses reference links to the
0008 %   original data
0009 
0010 arguments
0011    DataPaths
0012    ID (1,:) {mustBeNonzeroLengthText} % ID must be provided
0013    figures (1,:) {mustBeFigure} % Checks if Figures are figures
0014    options.Location {mustBeMember(options.Location ,['local','server','CI-Test'])} = 'local' % storage path
0015    options.Method {mustBeMember(options.Method ,['individual','centraliced'])} = 'individual'
0016    options.CopyUserFCN (1,1) {mustBeNumericOrLogical} = true 
0017 end
0018 
0019 switch options.Location 
0020     case 'local'
0021          storPath = fullfile(pwd,'export');
0022     case 'server'
0023         storPath = '\\FST-220\Jans-50GB-SMB\Lemmer';
0024     case 'CI-Test'
0025         storPath = fullfile(pwd,'CI_files','export');
0026 end
0027 folderName = char(ID);
0028 
0029 %% Create Data-Directory
0030 addpath(storPath);
0031 if mkdir(fullfile(storPath,folderName))
0032 else
0033     error('Directory could not be created - check remote path and permissions');
0034 end
0035 
0036 disp('Publishing started');
0037 
0038 %% Create a Copy of the script and User functions(optional)
0039 createFileCopy({[DataPaths.script,'.m']},folderName,storPath,ID, 'script');
0040 
0041 if options.CopyUserFCN
0042    [fList,pList] = matlab.codetools.requiredFilesAndProducts(DataPaths.script);
0043    % plist contains the required MATLAB Toolboxes, maybe usefull in future
0044    fList = fList(~ismember(fList,[DataPaths.script,'.m'])); % rmv script from list
0045    fList = removePltIdFiles(fList); % Do not copy files that are part of plot ID
0046    if ~isempty(fList)
0047        createFileCopy(fList,folderName,storPath,ID,'userFcn');
0048    end
0049 end
0050 
0051 %% Research data handeling
0052 switch options.Method
0053     case 'centraliced' % Work in progresss!!!
0054         %check if data folder exists
0055         if ~isfolder(fullfile(storPath,'data'))
0056             mkdir(fullfile(storPath,'data'));
0057         end
0058         %list all files
0059         fList = dir(fullfile(storPath,'data', '**\*.*'));  %get list of files and folders in any subfolder
0060         fList = fList(~[fList.isdir]);  %remove folders from list
0061         fList = struct2table(fList);
0062         
0063         % Check if the new plot is based on the original data-set
0064             % copy the data(once)
0065         for i=1:numel(DataPaths.rdata)            
0066             % check if identical file exists (status = 1)
0067             [status, idx] = fileCompare(DataPaths.rdata{i},fList);
0068             if status
0069                sourcePath = fList.name{idx};
0070                createLinkedHDF5(sourcePath,storPath,ID);
0071             else % no identical file exists
0072                createFileCopy(DataPaths.rdata,'data',storPath,ID,'dataCentral');
0073             end
0074         end
0075     case 'individual'
0076         % Create a copy of the research data
0077         createFileCopy(DataPaths.rdata,folderName,storPath,ID, 'data');
0078 end
0079 %% Export the Plots
0080 %try
0081    for i=1:numel(figures)
0082        fig = figures(i);
0083        PlotName = [ID,'_plot',num2str(i)]; % Der Plotnamen
0084        RemotePath = fullfile(storPath ,folderName, PlotName);
0085        % Matlab figure
0086        savefig(fig,RemotePath);
0087        exportgraphics(fig,[RemotePath,'.png'],'Resolution',300);
0088        disp([num2str(i),' of ',num2str(numel(figures)),' figures exported']);
0089    end
0090 %catch
0091     %warning('Plot export was not sucessful')
0092 %end
0093 
0094 disp(['publishing of ', ID , ' done']);
0095 
0096 end %function
0097 
0098 function [] = createFileCopy(filePaths,folderName,storPath,ID,type)
0099 % Creates a copy of the files (can used for multiple paths in a cell array)
0100 % folderName is the name of the exporting folder
0101     disp(['start to copy ', type]);  
0102             
0103 %     try
0104         for i = 1:numel(filePaths)
0105             FileNameAndLocation = filePaths{i};
0106             [~,name,ext] = fileparts(filePaths{i}); % get the extension
0107                           
0108             switch type
0109                 case 'data'
0110                     sufix = '_data';
0111                     newfile = sprintf([ID, sufix, '_' , num2str(i) ,ext]);
0112                 case 'dataCentral'
0113                     %keep original name
0114                     newfile = sprintf([name,ext]);
0115                 case 'script'
0116                     sufix = '_script';
0117                     newfile = sprintf([ID, sufix ,ext]);
0118                 case 'userFcn'
0119                     %keep original name
0120                     newfile = sprintf([name,ext]);
0121                 otherwise 
0122                     error([type,' is not a valid type for createFileCopy'])
0123             end %switch
0124             
0125             % Write the file
0126             RemotePath = fullfile(storPath,folderName, newfile);
0127             copyfile(FileNameAndLocation,RemotePath);
0128         end
0129         disp([type, ' sucessfully published']);
0130 %     catch
0131 %         warning([type,' export was not sucessful'])
0132 %    end %try
0133 end
0134 
0135 function tf = mustBeFigure(h)
0136 %checks if input is a figure object
0137   tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');
0138 end

Generated on Tue 03-Aug-2021 18:32:18 by m2html © 2005