Skip to content
Snippets Groups Projects

Add parent dir feature

5 files
+ 196
182
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 135
128
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
% remote path, that is defined in the config file.
% 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 (hdf5 only).
arguments
DataPaths
ID (1,:) {mustBeNonzeroLengthText} % ID must be provided
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','centralized'])} = 'individual'
options.CopyUserFCN (1,1) {mustBeNumericOrLogical} = true
options.CSV (1,1) {mustBeNumericOrLogical} = false
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'
% use the script path as export path
scriptPath = fileparts(DataPaths.script);
storPath = fullfile(scriptPath,'export');
case 'server' %from config File
txt = fileread('config.json');
config = jsondecode(txt);
storPath = config.ServerPath;
case 'CI-Test'
storPath = fullfile(pwd,'CI_files','export');
end
folderName = char(ID);
%% Create Data-Directory
addpath(storPath); % ToDo necessary? -
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
disp('Publishing started');
%% Create a Copy of the script and User functions(optional)
PlotID.createFileCopy({[DataPaths.script,'.m']},folderName,storPath,ID, 'script');
if options.CopyUserFCN
[fList,pList] = matlab.codetools.requiredFilesAndProducts(DataPaths.script);
% plist contains the required MATLAB Toolboxes, maybe usefull in future
fList = fList(~ismember(fList,[DataPaths.script,'.m'])); % rmv script from list
fList = PlotID.removePltIdFiles(fList); % Do not copy files that are part of plot ID
if ~isempty(fList)
PlotID.createFileCopy(fList,folderName,storPath,ID,'userFcn');
end
end
%% Research data handeling
switch options.Method
case 'centralized'
% check if data folder exists
if ~isfolder(fullfile(storPath,'data'))
mkdir(fullfile(storPath,'data'));
end
%list all files
fList = dir(fullfile(storPath,'data', '**\*.*')); %get list of files and folders in any subfolder
fList = fList(~[fList.isdir]); %remove folders from list
fList = struct2table(fList);
% Check if the new plot is based on the original data-set
% copy the data(once)
for i=1:numel(DataPaths.rdata)
% check if identical file exists (status = 1)
[~, idx] = PlotID.fileCompare(DataPaths.rdata{i},fList);
% create Linked HDF5 files for identical files
if any(idx)
sourcePath = fList{idx,'name'}; % If there are multiple copies already, this only picks the last entry
if contains(sourcePath,{'.h5','.hdf5'}) % Linking only for HDF5
PlotID.createLinkedHDF5(sourcePath,storPath,ID);
end
else % no identical file exists
PlotID.createFileCopy(DataPaths.rdata{i},'data',storPath,ID,'dataCentral');
end
end
case 'individual'
% Create a copy of the research data
PlotID.createFileCopy(DataPaths.rdata,folderName,storPath,ID, 'data');
end
%% Export the Plot
try
PlotName = [ID,'_plot']; % plotname
RemotePath = fullfile(storPath ,folderName, PlotName);
% Matlab figure
savefig(figure,RemotePath);
% the png should only be a preview
exportgraphics(figure,[RemotePath,'.png'],'Resolution',300);
catch
warning('Plot export was not successful')
end
disp(['publishing of ', ID , ' done']);
% CSV EXport
if options.CSV
T = table();
T.research_Data = DataPaths.rdata'; T.PlotID(:) = {ID};
T.Script_Name(:) = {[DataPaths.script,'.m']};
T.Storage_Location(:) = {storPath};
T.Date(:) = {datestr(now)};
T = movevars(T,'PlotID','before',1);
writetable(T, fullfile(storPath, 'overview_table.csv'),'WriteMode','append');
end
end %function
function tf = mustBeFigure(h)
%checks if input is a figure object
tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');
end
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
% remote path, that is defined in the config file.
% 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 (hdf5 only).
% ParentFolder is the folder Name where the exported data is stored if an
% path is used, PlotId will use this path a storagePath
arguments
DataPaths
ID (1,:) {mustBeNonzeroLengthText} % ID must be provided
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.ParentFolder (1,:) {mustBeText} = 'export'
options.CopyUserFCN (1,1) {mustBeNumericOrLogical} = true
options.CSV (1,1) {mustBeNumericOrLogical} = false
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'
if contains(options.ParentFolder, {'/','\'})
storPath = options.ParentFolder;
else
% use the script path as export path
scriptPath = fileparts(DataPaths.script);
storPath = fullfile(scriptPath,options.ParentFolder);
end
case 'server' %from config File
txt = fileread('config.json');
config = jsondecode(txt);
storPath = config.ServerPath;
case 'CI-Test'
storPath = fullfile(pwd,'CI_files',options.ParentFolder);
end
folderName = char(ID);
%% Create Data-Directory
addpath(storPath); % ToDo necessary? -
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
disp('Publishing started');
%% Create a Copy of the script and User functions(optional)
PlotID.createFileCopy({[DataPaths.script,'.m']},folderName,storPath,ID, 'script');
if options.CopyUserFCN
[fList,pList] = matlab.codetools.requiredFilesAndProducts(DataPaths.script);
% plist contains the required MATLAB Toolboxes, maybe usefull in future
fList = fList(~ismember(fList,[DataPaths.script,'.m'])); % rmv script from list
fList = PlotID.removePltIdFiles(fList); % Do not copy files that are part of plot ID
if ~isempty(fList)
PlotID.createFileCopy(fList,folderName,storPath,ID,'userFcn');
end
end
%% Research data handeling
switch options.Method
case 'centraliced'
% check if data folder exists
if ~isfolder(fullfile(storPath,'data'))
mkdir(fullfile(storPath,'data'));
end
%list all files
fList = dir(fullfile(storPath,'data', '**\*.*')); %get list of files and folders in any subfolder
fList = fList(~[fList.isdir]); %remove folders from list
fList = struct2table(fList);
% Check if the new plot is based on the original data-set
% copy the data(once)
for i=1:numel(DataPaths.rdata)
% check if identical file exists (status = 1)
[~, idx] = PlotID.fileCompare(DataPaths.rdata{i},fList);
% create Linked HDF5 files for identical files
if any(idx)
sourcePath = fList{idx,'name'}; % If there are multiple copies already, this only picks the last entry
if contains(sourcePath,{'.h5','.hdf5'}) % Linking only for HDF5
PlotID.createLinkedHDF5(sourcePath,storPath,ID);
end
else % no identical file exists
PlotID.createFileCopy(DataPaths.rdata{i},'data',storPath,ID,'dataCentral');
end
end
case 'individual'
% Create a copy of the research data
PlotID.createFileCopy(DataPaths.rdata,folderName,storPath,ID, 'data');
end
%% Export the Plot
try
PlotName = [ID,'_plot']; % plotname
RemotePath = fullfile(storPath ,folderName, PlotName);
% Matlab figure
savefig(figure,RemotePath);
% the png should only be a preview
exportgraphics(figure,[RemotePath,'.png'],'Resolution',300);
catch
warning('Plot export was not successful')
end
disp(['publishing of ', ID , ' done']);
% CSV EXport
if options.CSV
T = table();
T.research_Data = DataPaths.rdata'; T.PlotID(:) = {ID};
T.Script_Name(:) = {[DataPaths.script,'.m']};
T.Storage_Location(:) = {storPath};
T.Date(:) = {datestr(now)};
T = movevars(T,'PlotID','before',1);
writetable(T, fullfile(storPath, 'overview_table.csv'),'WriteMode','append');
end
end %function
function tf = mustBeFigure(h)
%checks if input is a figure object
tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');
end
Loading