From 37dce9de119791bd6aa81f1c9abbfa341a204d5f Mon Sep 17 00:00:00 2001 From: giuliaghirardini <giulia.ghirardini@skywarder.eu> Date: Fri, 28 Mar 2025 18:14:44 +0100 Subject: [PATCH] [fixes] Converted global variables into function arguements and divided code into functions --- .../fct/autoNameGenerator.m | 3 + .../fct/emptyNameField.m | 9 + .../fct/exportFigureCallback.m | 60 +++ .../fct/findFigures.m | 4 + .../fct/getFigureNames.m | 11 + .../fct/importSettings.m | 28 ++ .../fct/limitsCheck.m | 9 + .../fct/previewFigureCallback.m | 35 ++ .../fct/resetToDefaultAll.m | 20 + .../fct/resetToDefaultMini.m | 5 + .../fct/saveSettings.m | 32 ++ .../fct/saveTemporaryFigure.m | 19 + .../fct/updateDropdown.m | 11 + .../fct/updatePreview.m | 114 +++++ .../mainExportStandardizedFigure.m | 449 ++---------------- 15 files changed, 400 insertions(+), 409 deletions(-) create mode 100644 functions/utilities/exportStandardizedFigure/fct/autoNameGenerator.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/emptyNameField.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/findFigures.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/getFigureNames.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/importSettings.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/limitsCheck.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/resetToDefaultAll.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/resetToDefaultMini.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/saveSettings.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/saveTemporaryFigure.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/updateDropdown.m create mode 100644 functions/utilities/exportStandardizedFigure/fct/updatePreview.m diff --git a/functions/utilities/exportStandardizedFigure/fct/autoNameGenerator.m b/functions/utilities/exportStandardizedFigure/fct/autoNameGenerator.m new file mode 100644 index 0000000..98ad5df --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/autoNameGenerator.m @@ -0,0 +1,3 @@ +function autoNameGenerator(inputFields) + inputFields.figName.Value = "plot"; + end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/emptyNameField.m b/functions/utilities/exportStandardizedFigure/fct/emptyNameField.m new file mode 100644 index 0000000..72d5090 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/emptyNameField.m @@ -0,0 +1,9 @@ +function emptyNameField(src) + if isempty(src.Value) + src.Tooltip = 'Insert a valid name'; + src.BackgroundColor = [1, 0.8, 0.8]; % Light red + else + src.Tooltip = ''; + src.BackgroundColor = [1, 1, 1]; % White + end + end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m b/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m new file mode 100644 index 0000000..2fd7904 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m @@ -0,0 +1,60 @@ +% Callback Function for Button + function exportFigureCallback(fig, box, dropDown, inputFields) + % Get values from the GUI components + figName = [dropDown.prefixIPT.Value inputFields.figName.Value]; + percTextwidth = inputFields.percText.Value; + addMarkers = box.addMarkers.Value; + forcedMarkers = inputFields.forcedMarkers.Value; + changeColors = box.changeColors.Value; + changeLineStyle = box.changeLineStyle.Value; + gridOption = box.grid.Value; + legendLocation = dropDown.legendLocation.Value; + legendOrientation = dropDown.legendOrientation.Value; + exportPDF = box.exportPDF.Value; + exportFIG = box.exportFIG.Value; + satelliteMapColors = box.satelliteMapColors.Value; + % figurePath = pathField.Value; + WHratio = inputFields.WHratio.Value; + overwriteFigure = box.overwriteFigure.Value; + + % Check for required inputs + if isempty(figName) || isempty(percTextwidth) + uialert(fig, 'Please enter both Figure Name and Percentage Text Width.', 'Input Error'); + return; + end + + % Figure path + figurePath = uigetdir; + + % Cache figure to reset easily + selectedIdx = dropDown.figSelection.Value; + figHandles = findFigures(fig); + figToExp = figHandles(strcmp(dropDown.figSelection.Items, selectedIdx)); + + % currentPath = mfilename('fullpath'); + % cachePath = fullfile(currentPath, '..','cachedFigs', selectedIdx); + % savefig(figToExp, cachePath); + + % Try to export figure + try + % Call the exportStandardizedFigure function + exportStandardizedFigure(figToExp, figName, percTextwidth, ... + 'addMarkers', addMarkers, ... + 'forcedMarkers', forcedMarkers, ... + 'changeColors', changeColors, ... + 'changeLineStyle', changeLineStyle, ... + 'grid', gridOption, ... + 'legendLocation', legendLocation, ... + 'legendOrientation', legendOrientation, ... + 'exportPDF', exportPDF, ... + 'exportFIG', exportFIG, ... + 'satelliteMapColors', satelliteMapColors, ... + 'figurePath', figurePath, ... + 'WHratio', WHratio, ... + 'overwriteFigure', overwriteFigure); + uialert(fig, 'Figure exported successfully!', 'Success', 'Icon', 'success'); + catch ME + % Display error if the function fails + uialert(fig, ['Error exporting figure: ' ME.message], 'Error'); + end + end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/findFigures.m b/functions/utilities/exportStandardizedFigure/fct/findFigures.m new file mode 100644 index 0000000..377b8d0 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/findFigures.m @@ -0,0 +1,4 @@ +function figHandles = findFigures(fig) +figHandles = findall(0, 'Type', 'figure'); +figHandles(figHandles == fig) = []; % Exclude GUI figure +end diff --git a/functions/utilities/exportStandardizedFigure/fct/getFigureNames.m b/functions/utilities/exportStandardizedFigure/fct/getFigureNames.m new file mode 100644 index 0000000..1392f4f --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/getFigureNames.m @@ -0,0 +1,11 @@ +function figNames = getFigureNames(figHandles) +if isempty(figHandles) + figNames = {'No figures open'}; +else + figNames = arrayfun(@(f) sprintf('%s', f.Name), figHandles, 'UniformOutput', false); + % Replace empty names with "Figure #" + emptyNames = cellfun(@isempty, figNames); + figNames(emptyNames) = arrayfun(@(f) ... + sprintf('Figure %d', f.Number), figHandles(emptyNames), 'UniformOutput', false); +end + diff --git a/functions/utilities/exportStandardizedFigure/fct/importSettings.m b/functions/utilities/exportStandardizedFigure/fct/importSettings.m new file mode 100644 index 0000000..5189983 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/importSettings.m @@ -0,0 +1,28 @@ +function importSettings(box, dropDown, inputField) +[fileSettings, pathSettings] = uigetfile('*.json'); +fileName = fullfile(pathSettings,fileSettings); +fid = fopen(fileName); +str = char(fread(fid,inf)'); +fclose(fid); +disp('File opened correctly'); + +settings = jsondecode(str); + +% Apply values +box.addMarkers.Value = settings.addMarkers; +box.changeColors.Value = settings.changeColors; +box.changeLineStyle.Value = settings.changeLineStyle; +box.grid.Value = settings.gridOption; +box.satelliteMapColors.Value = settings.satelliteMapColors; + +dropDown.legendLocation.Value = settings.legendLocation; +dropDown.legendOrientation.Value = settings.legendOrientation; + +box.exportPDF.Value = settings.exportPDF; +box.exportFIG.Value = settings.exportFIG; +box.overwriteFigure.Value = settings.overwriteFigure; + +inputField.percText.Value = settings.percTextwidth; +inputField.forcedMarkers.Value = settings.forcedMarkers; +inputField.WHratio.Value = settings.WHratio; +end diff --git a/functions/utilities/exportStandardizedFigure/fct/limitsCheck.m b/functions/utilities/exportStandardizedFigure/fct/limitsCheck.m new file mode 100644 index 0000000..38d2c3d --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/limitsCheck.m @@ -0,0 +1,9 @@ +function limitsCheck(src, event) +if event.Value > 1 + src.Tooltip = 'Only values less than 1 are accepted'; + src.BackgroundColor = [1, 0.8, 0.8]; % Light red +else + src.Tooltip = ''; + src.BackgroundColor = [1, 1, 1]; % White +end +end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m b/functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m new file mode 100644 index 0000000..28e3278 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m @@ -0,0 +1,35 @@ +function previewFigureCallback(fig, box, dropDown, inputFields) + % Get values from the GUI components + percTextwidth = inputFields.percText.Value; + addMarkers = box.addMarkers.Value; + forcedMarkers = inputFields.forcedMarkers.Value; + changeColors = box.changeColors.Value; + changeLineStyle = box.changeLineStyle.Value; + gridOption = box.grid.Value; + legendLocation = dropDown.legendLocation.Value; + legendOrientation = dropDown.legendOrientation.Value; + exportPDF = box.exportPDF.Value; + exportFIG = box.exportFIG.Value; + satelliteMapColors = box.satelliteMapColors.Value; + % figurePath = pathField.Value; + WHratio = inputFields.WHratio.Value; + overwriteFigure = box.overwriteFigure.Value; + + selectedIdx = dropDown.figSelection.Value; + figHandles = findFigures(fig); + figToExp = figHandles(strcmp(dropDown.figSelection.Items, selectedIdx)); + + previewStandardizedFigure(figToExp, percTextwidth, ... + 'addMarkers', addMarkers, ... + 'forcedMarkers', forcedMarkers, ... + 'changeColors', changeColors, ... + 'changeLineStyle', changeLineStyle, ... + 'grid', gridOption, ... + 'legendLocation', legendLocation, ... + 'legendOrientation', legendOrientation, ... + 'exportPDF', exportPDF, ... + 'exportFIG', exportFIG, ... + 'satelliteMapColors', satelliteMapColors, ... + 'WHratio', WHratio, ... + 'overwriteFigure', overwriteFigure); + end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/resetToDefaultAll.m b/functions/utilities/exportStandardizedFigure/fct/resetToDefaultAll.m new file mode 100644 index 0000000..d4b3b9e --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/resetToDefaultAll.m @@ -0,0 +1,20 @@ +function resetToDefaultAll(box, dropDown, inputFields) + dropDown.prefixIPTName.Value = '[]'; + inputFields.figName.Value = ''; + inputFields.forcedMarkers.Value = 0; + inputFields.WHratio.Value = 0; + inputFields.percText.Value = 0.75; + % Flags default settings + box.addMarkers.Value = true; + box.changeColors.Value = true; + box.changeLineStyle.Value = false; + box.grid.Value = true; + box.satelliteMapColors.Value = false; + % Legend default settings + dropDown.legendLocation.Value = 'southoutside'; + dropDown.legendOrientation.Value = 'horizontal'; + % Export default settings + box.overwriteFigure.Value = false; + box.exportPDF.Value = true; + box.exportFIG.Value = false; + end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/resetToDefaultMini.m b/functions/utilities/exportStandardizedFigure/fct/resetToDefaultMini.m new file mode 100644 index 0000000..e79757a --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/resetToDefaultMini.m @@ -0,0 +1,5 @@ + function resetToDefaultMini(inputFields) + inputFields.forcedMarkers.Value = 0; + inputFields.WHratio.Value = 0; + inputFields.percText.Value = 0.75; + end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/saveSettings.m b/functions/utilities/exportStandardizedFigure/fct/saveSettings.m new file mode 100644 index 0000000..abac24b --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/saveSettings.m @@ -0,0 +1,32 @@ +function saveSettings(box, dropDown, inputField) +% This function save all the settings in a json file + +settings.addMarkers = box.addMarkers.Value; +settings.changeColors = box.changeColors.Value; +settings.changeLineStyle = box.changeLineStyle.Value; +settings.gridOption = box.grid.Value; +settings.satelliteMapColors = box.satelliteMapColors.Value; + +settings.legendLocation = dropDown.legendLocation.Value; +settings.legendOrientation = dropDown.legendOrientation.Value; + +settings.exportPDF = box.exportPDF.Value; +settings.exportFIG = box.exportFIG.Value; +settings.overwriteFigure = box.overwriteFigure.Value; + +settings.percTextwidth = inputField.percText.Value; +settings.forcedMarkers = inputField.forcedMarkers.Value; +settings.WHratio = inputField.WHratio.Value; + +filter = '*.json'; +[fileName, location] = uiputfile(filter); + +encoded = jsonencode(settings,PrettyPrint=true); +name = ['settings_' fileName]; +fullNameFile = fullfile(location, name); +fid = fopen(fullNameFile,'w'); +fprintf(fid,'%s',encoded); +fclose(fid); + +disp('Settings saved correctly'); +end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/saveTemporaryFigure.m b/functions/utilities/exportStandardizedFigure/fct/saveTemporaryFigure.m new file mode 100644 index 0000000..d3af9b6 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/saveTemporaryFigure.m @@ -0,0 +1,19 @@ +function saveTemporaryFigure(fig) + % retrieve local path to save temp fig in temp folder + fullPath = fileparts(mfilename('fullpath')); + parentPath = fileparts(fullPath); % Remove the last folder + tempPath = [parentPath '\temp']; + if ~isfolder(tempPath) + mkdir(tempPath); + end + + figToExp = findFigures(fig); + names = getFigureNames(figToExp); + + for i=1:length(names) + filename = [tempPath '\' char(names(i))]; + savefig(figToExp(i), filename) + end + disp('Temporary .fig available in temp folder'); +end + diff --git a/functions/utilities/exportStandardizedFigure/fct/updateDropdown.m b/functions/utilities/exportStandardizedFigure/fct/updateDropdown.m new file mode 100644 index 0000000..45ef967 --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/updateDropdown.m @@ -0,0 +1,11 @@ +function updateDropdown(fig, flagsPanel, screen, dropDown, box, inputFields) +figHandles = findFigures(fig); +figNames = getFigureNames(figHandles); + +dropDown.figSelection.Items = figNames; +saveTemporaryFigure(fig); +% Automatically update preview if only one valid figure exists +if isscalar(figHandles) + updatePreview(fig, flagsPanel, screen, dropDown, box, inputFields); +end +end \ No newline at end of file diff --git a/functions/utilities/exportStandardizedFigure/fct/updatePreview.m b/functions/utilities/exportStandardizedFigure/fct/updatePreview.m new file mode 100644 index 0000000..0b7940a --- /dev/null +++ b/functions/utilities/exportStandardizedFigure/fct/updatePreview.m @@ -0,0 +1,114 @@ +% Function to update preview + function updatePreview(fig, flagsPanel, screen, dropDown, box, inputFields) + checkboxes = findall(flagsPanel.Children, 'Type', 'uicheckbox'); + set(checkboxes, 'Enable', 'on'); + + figHandles = findFigures(fig); + + % Get selected figure from dropdown + selectedIdx = dropDown.figSelection.Value; + selectedFig = []; + + for i = 1:length(figHandles) + figSeries = figHandles(i); + + % Set the default figure position + pos = get(figSeries, 'Position'); % gives x left, y bottom, width, height + width = pos(3); + height = pos(4); + leftOffsetNew = screen.screenWidth/5; + topOffsetNew = screen.screenHeight/3; + + positionCurrentFig = [screen.screenWidth-width-leftOffsetNew, ... + screen.screenHeight-height-topOffsetNew, width, height]; + set(figSeries, 'Position', positionCurrentFig); + end + + % Find the selected figure + for i = 1:length(figHandles) + if strcmp(dropDown.figSelection.Items{i}, selectedIdx) + selectedFig = figHandles(i); + break; + end + end + % Move from background to foreground current figure + uistack(selectedFig,'top') + + if isempty(selectedFig) && isscalar(figHandles), selectedFig = figHandles(1); end + if isempty(selectedFig) || ~isvalid(selectedFig), return; end + + % Retrieve axes from selected figure + obj = findall(selectedFig, 'Type', 'figure'); + + % Copy the axes informations + check = size(obj.Children); + switch check(1) + case 0 + return; + case 1 + if box.satelliteMapColors.Value, box.satelliteMapColors.Value = false; end + + newAx = obj.Children; + hold(newAx, 'on'); + case 2 + if isa(obj.Children(1), 'matlab.ui.container.TabGroup') + if box.satelliteMapColors.Value, box.satelliteMapColors.Value = false; end + + numTabs = numel(obj.Children(1).Children); % Get number of tabs + originalFigNum = obj.Number; % Get the original figure number + + for i = 1:numTabs + newFigName = sprintf('Figure %d - Tab %d', originalFigNum, i); + newFig = figure('Name', newFigName, 'NumberTitle', 'off'); + + newAx = copyobj(obj.Children(1).Children(i).Children, newFig); + + set(newAx, 'Position', get(gca, 'Position')); + hold(newAx, 'on'); + end + delete(obj); + else + if strcmp(obj.Children(2).Type, 'geoaxes') + box.satelliteMapColors.Value = true; + else + box.satelliteMapColors.Value = false; + end + newAx = obj.Children(2); + hold(newAx, 'on'); + end + otherwise + if isa(obj.Children(1), 'matlab.graphics.illustration.Legend') + if box.satelliteMapColors.Value, box.satelliteMapColors.Value = false; end + + newAx = obj.Children(2:end); + hold(newAx(1:check(1)-1), 'on'); + else + if box.satelliteMapColors.Value, box.satelliteMapColors.Value = false; end + + newAx = obj.Children; + hold(newAx, 'on'); + end + end + + % Apply user settings + linesInPlot = findall(newAx, 'Type', 'line'); + + if box.changeColors.Value, set(newAx, 'ColorOrder', lines(7)); end + if box.changeLineStyle.Value, set(linesInPlot, 'LineStyle', '--'); end + if box.satelliteMapColors.Value, colormap(newAx, 'parula'); end + if inputFields.WHratio.Value > 0, daspect(newAx, [1 inputFields.WHratio.Value 1]); end + + if box.changeLineStyle.Value + for line = linesInPlot' + set(line, 'LineStyle', '--'); + end + end + + if box.grid.Value + grid(newAx, 'on'); + else + grid(newAx, 'off'); + end + + hold(newAx, 'off'); + end diff --git a/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m b/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m index f437219..0d3228f 100644 --- a/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m +++ b/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m @@ -16,15 +16,15 @@ figY = (screenSize(4) - figHeight) / 2; % Get the screen size screenSize = get(0, 'ScreenSize'); -screenWidth = screenSize(3); -screenHeight = screenSize(4); +screen.screenWidth = screenSize(3); +screen.screenHeight = screenSize(4); % Define an offset from the top of the screen topOffset = figY; leftOffset = figX; % Calculate the position for the new figures -positionGui = [leftOffset, screenHeight-figHeight-topOffset, figWidth, figHeight]; +positionGui = [leftOffset, screen.screenHeight-figHeight-topOffset, figWidth, figHeight]; %% Create main figure window fig = uifigure('Name', 'Export Standardized Figure', 'Position', positionGui); @@ -65,6 +65,12 @@ uilabel(fig, 'Text', 'Percentage of Text Width:', 'Position', [450 170 200 30]); percTextField = uieditfield(fig, 'numeric', 'Value', 0.75, 'Position', [600 170 50 30], ... 'ValueChangedFcn', @(src, event) limitsCheck(src, event)); +%%% save into a structure +inputFields.figName = figNameField; +inputFields.WHratio = WHratioField; +inputFields.forcedMarkers = forcedMarkersField; +inputFields.percText = percTextField; + %% Checkboxes %%% Create a panel to group flags flagsPanel = uipanel(fig, ... @@ -100,6 +106,16 @@ exportFIGBox = uicheckbox(exportPanel, 'Text', 'Export FIG', 'Value', false, ... overwriteFigureBox = uicheckbox(exportPanel, 'Text', 'Overwrite', 'Value', false, ... 'Position', [240 10 150 20]); +%%% save box data into struct +box.addMarkers = addMarkersBox; +box.changeColors = changeColorsBox; +box.changeLineStyle = changeLineStyleBox; +box.grid = gridBox; +box.satelliteMapColors = satelliteMapColorsBox; +box.exportPDF = exportPDFBox; +box.exportFIG = exportFIGBox; +box.overwriteFigure = overwriteFigureBox; + %% Dropdown menus %%% Create a panel to group dropdown menus legendPanel = uipanel(fig, ... @@ -125,6 +141,10 @@ legendOrientationDropDown = uidropdown(legendPanel, 'Items', legendOrientations, 'Value', 'horizontal', ... 'Position', [330 10 150 22]); +%%% save dropdown into a structure +dropDown.legendOrientation= legendOrientationDropDown; +dropDown.legendLocation = legendLocationDropDown; + %% Preview content % Find all open figures (excluding this GUI) figHandles = findFigures(fig); @@ -136,13 +156,17 @@ figSelectionDropDown = uidropdown(fig, ... 'Items', figNames, ... 'Position', [150, 500, 200, 30], ... 'Tooltip', 'Select the figure you want to export', ... - 'ValueChangedFcn', @(src, event) updatePreview()); + 'ValueChangedFcn', @(src, event) updatePreview(fig, flagsPanel, screen, dropDown, box, inputFields)); + +%%% save dropdown into a structure +dropDown.prefixIPT = prefixIPTNameDropDown; +dropDown.figSelection = figSelectionDropDown; % Automatically update preview if only one figure exists if isscalar(figHandles) - saveTemporaryFigure(); - updateDropdown(); - updatePreview(); + saveTemporaryFigure(fig); + updateDropdown(fig, flagsPanel, screen, dropDown, box, inputFields); + updatePreview(fig, flagsPanel, screen, dropDown, box, inputFields); end %% Buttons @@ -151,28 +175,28 @@ updateButton = uibutton(fig, 'Text', 'Update Figures', ... 'Position', [370, 500, 120, 30], ... 'BackgroundColor', '#9fc9eb', ... 'Tooltip', 'Update list of available figures', ... - 'ButtonPushedFcn', @(src, event) updateDropdown()); + 'ButtonPushedFcn', @(src, event) updateDropdown(fig, flagsPanel, screen, dropDown, box, inputFields)); % preview image to export button previewButton = uibutton(fig, 'Text', 'Update preview', ... 'Position', [640, 500, 120, 30], ... 'BackgroundColor', '#fdf3ae', ... 'Tooltip', 'Click to see a preview of your plot', ... - 'ButtonPushedFcn', @(src, event) previewFigureCallback()); + 'ButtonPushedFcn', @(src, event) previewFigureCallback(fig, box, dropDown, inputFields)); % return to default button only for last 3 fields defaultButton = uibutton(fig, 'Text', 'Reset', ... 'Position', [710, 170, 50, 30], ... 'BackgroundColor', [.7 .7 .7], ... 'Tooltip', 'Return to default values', ... - 'ButtonPushedFcn', @(src, event) resetToDefaultMini()); + 'ButtonPushedFcn', @(src, event) resetToDefaultMini(inputFields)); % automatically generate a name autoNameButton = uibutton(fig, 'Text', 'Auto name', ... 'Position', [640, 550, 120, 30], ... 'BackgroundColor', [.7 .7 .7], ... 'Tooltip', 'Generate a automatic name: "plot"', ... - 'ButtonPushedFcn', @(src, event) autoNameGenerator()); + 'ButtonPushedFcn', @(src, event) autoNameGenerator(inputFields)); % -------------------- BOTTOM PART --------------------- % Export Button @@ -180,420 +204,27 @@ exportButton = uibutton(fig, 'Text', 'Export Figure', ... 'Position', [130 100 100 30], ... 'BackgroundColor', '#cbffbe', ... 'Tooltip', 'Click to export', ... - 'ButtonPushedFcn', @(src, event) exportFigureCallback()); + 'ButtonPushedFcn', @(src, event) exportFigureCallback(fig, box, dropDown, inputFields)); % Save settings Button settingsSaveButton = uibutton(fig, 'Text', 'Save settings', ... 'Position', [270 100 100 30], ... 'BackgroundColor', '#cbffbe', ... 'Tooltip', 'Save your configuration in .json file', ... - 'ButtonPushedFcn', @(src, event) saveSettings()); + 'ButtonPushedFcn', @(src, event) saveSettings(box, dropDown, inputFields)); % Import settings Button settingsImportButton = uibutton(fig, 'Text', 'Import settings', ... 'Position', [410 100 100 30], ... 'BackgroundColor', '#9fc9eb', ... 'Tooltip', 'Import settings from .json file', ... - 'ButtonPushedFcn', @(src, event) importSettings()); + 'ButtonPushedFcn', @(src, event) importSettings(box, dropDown, inputFields)); % Reset to default settings to start over allDefaultButton = uibutton(fig, 'Text', 'Reset All', ... 'Position', [550 100 100 30], ... 'BackgroundColor', '#d99779', ... 'Tooltip', 'Reset all settings to default', ... - 'ButtonPushedFcn', @(src, event) resetToDefaultAll()); - -%% Functions -% Function to update dropdown menu with new figures - function importSettings() - [fileSettings, pathSettings] = uigetfile('*.json'); - fileName = fullfile(pathSettings,fileSettings); - fid = fopen(fileName); - str = char(fread(fid,inf)'); - fclose(fid); - settings = jsondecode(str); - - % Apply values - addMarkersBox.Value = settings.addMarkers; - changeColorsBox.Value = settings.changeColors; - changeLineStyleBox.Value = settings.changeLineStyle; - gridBox.Value = settings.gridOption; - satelliteMapColorsBox.Value = settings.satelliteMapColors; - - legendLocationDropDown.Value = settings.legendLocation; - legendOrientationDropDown.Value = settings.legendOrientation; - - exportPDFBox.Value = settings.exportPDF; - exportFIGBox.Value = settings.exportFIG; - overwriteFigureBox.Value = settings.overwriteFigure; - - percTextField.Value = settings.percTextwidth; - forcedMarkersField.Value = settings.forcedMarkers; - WHratioField.Value = settings.WHratio; - end - - function saveSettings() - % This function save all the settings in a json file - - settings.addMarkers = addMarkersBox.Value; - settings.changeColors = changeColorsBox.Value; - settings.changeLineStyle = changeLineStyleBox.Value; - settings.gridOption = gridBox.Value; - settings.satelliteMapColors = satelliteMapColorsBox.Value; - - settings.legendLocation = legendLocationDropDown.Value; - settings.legendOrientation = legendOrientationDropDown.Value; - - settings.exportPDF = exportPDFBox.Value; - settings.exportFIG = exportFIGBox.Value; - settings.overwriteFigure = overwriteFigureBox.Value; - - settings.percTextwidth = percTextField.Value; - settings.forcedMarkers = forcedMarkersField.Value; - settings.WHratio = WHratioField.Value; - - filter = '*.json'; - [fileName, location] = uiputfile(filter); - - encoded = jsonencode(settings,PrettyPrint=true); - name = ['settings_' fileName]; - fullNameFile = fullfile(location, name); - fid = fopen(fullNameFile,'w'); - fprintf(fid,'%s',encoded); - fclose(fid); - end - - function autoNameGenerator() - figNameField.Value = "plot"; - end - - function emptyNameField(src) - if isempty(src.Value) - src.Tooltip = 'Insert a valid name'; - src.BackgroundColor = [1, 0.8, 0.8]; % Light red - else - src.Tooltip = ''; - src.BackgroundColor = [1, 1, 1]; % White - end - end - - function limitsCheck(src, event) - if event.Value > 1 - src.Tooltip = 'Only values less than 1 are accepted'; - src.BackgroundColor = [1, 0.8, 0.8]; % Light red - else - src.Tooltip = ''; - src.BackgroundColor = [1, 1, 1]; % White - end - end - - function resetToDefaultMini() - forcedMarkersField.Value = 0; - WHratioField.Value = 0; - percTextField.Value = 0.75; - end - - function resetToDefaultAll() - prefixIPTNameDropDown.Value = '[]'; - figNameField.Value = ''; - forcedMarkersField.Value = 0; - WHratioField.Value = 0; - percTextField.Value = 0.75; - % Flags default settings - addMarkersBox.Value = true; - changeColorsBox.Value = true; - changeLineStyleBox.Value = false; - gridBox.Value = true; - satelliteMapColorsBox.Value = false; - % Legend default settings - legendLocationDropDown.Value = 'southoutside'; - legendOrientationDropDown.Value = 'horizontal'; - % Export default settings - overwriteFigureBox.Value = false; - exportPDFBox.Value = true; - exportFIGBox.Value= false; - end - - function updateDropdown() - figHandles = findFigures(fig); - figNames = getFigureNames(figHandles); - - figSelectionDropDown.Items = figNames; - saveTemporaryFigure(); - % Automatically update preview if only one valid figure exists - if isscalar(figHandles) - updatePreview(); - end - end - -% Function to update preview - function updatePreview() - checkboxes = findall(flagsPanel.Children, 'Type', 'uicheckbox'); - set(checkboxes, 'Enable', 'on'); - - figHandles = findFigures(fig); - - % Get selected figure from dropdown - selectedIdx = figSelectionDropDown.Value; - selectedFig = []; - - for i = 1:length(figHandles) - figSeries = figHandles(i); - - % Set the default figure position - pos = get(figSeries, 'Position'); % gives x left, y bottom, width, height - width = pos(3); - height = pos(4); - leftOffsetNew = screenWidth/5; - topOffsetNew = screenHeight/3; - - positionCurrentFig = [screenWidth-width-leftOffsetNew, ... - screenHeight-height-topOffsetNew, width, height]; - set(figSeries, 'Position', positionCurrentFig); - end - - % Find the selected figure - for i = 1:length(figHandles) - if strcmp(figSelectionDropDown.Items{i}, selectedIdx) - selectedFig = figHandles(i); - break; - end - end - % Move from background to foreground current figure - uistack(selectedFig,'top') - % updateDropdown(); - - if isempty(selectedFig) && isscalar(figHandles), selectedFig = figHandles(1); end - if isempty(selectedFig) || ~isvalid(selectedFig), return; end - - % Retrieve axes from selected figure - obj = findall(selectedFig, 'Type', 'figure'); - - % Copy the axes informations - check = size(obj.Children); - switch check(1) - case 0 - return; - case 1 - if satelliteMapColorsBox.Value, satelliteMapColorsBox.Value = false; end - - newAx = obj.Children; - hold(newAx, 'on'); - case 2 - if isa(obj.Children(1), 'matlab.ui.container.TabGroup') - if satelliteMapColorsBox.Value, satelliteMapColorsBox.Value = false; end - - numTabs = numel(obj.Children(1).Children); % Get number of tabs - originalFigNum = obj.Number; % Get the original figure number - - for i = 1:numTabs - newFigName = sprintf('Figure %d - Tab %d', originalFigNum, i); - newFig = figure('Name', newFigName, 'NumberTitle', 'off'); - - newAx = copyobj(obj.Children(1).Children(i).Children, newFig); - - set(newAx, 'Position', get(gca, 'Position')); - hold(newAx, 'on'); - end - delete(obj); - else - if strcmp(obj.Children(2).Type, 'geoaxes') - satelliteMapColorsBox.Value = true; - else - satelliteMapColorsBox.Value = false; - end - newAx = obj.Children(2); - hold(newAx, 'on'); - end - otherwise - if isa(obj.Children(1), 'matlab.graphics.illustration.Legend') - if satelliteMapColorsBox.Value, satelliteMapColorsBox.Value = false; end - - newAx = obj.Children(2:end); - hold(newAx(1:check(1)-1), 'on'); - else - if satelliteMapColorsBox.Value, satelliteMapColorsBox.Value = false; end - - newAx = obj.Children; - hold(newAx, 'on'); - end - end - - % Apply user settings - linesInPlot = findall(newAx, 'Type', 'line'); - - % if addMarkersBox.Value - % for line = linesInPlot' - % set(line, 'Marker', 'o', 'MarkerSize', 4); - % end - % end - - if changeColorsBox.Value, set(newAx, 'ColorOrder', lines(7)); end - if changeLineStyleBox.Value, set(linesInPlot, 'LineStyle', '--'); end - if satelliteMapColorsBox.Value, colormap(newAx, 'parula'); end - if WHratioField.Value > 0, daspect(newAx, [1 WHratioField.Value 1]); end - - if changeLineStyleBox.Value - for line = linesInPlot' - set(line, 'LineStyle', '--'); - end - end - - if gridBox.Value - grid(newAx, 'on'); - else - grid(newAx, 'off'); - end - - % Ensure preview updates on checkbox change - % addMarkersBox.ValueChangedFcn = @(src, event) updatePreview(); - % changeColorsBox.ValueChangedFcn = @(src, event) updatePreview(); - % gridBox.ValueChangedFcn = @(src, event) updatePreview(); - % WHratioField.ValueChangedFcn = @(src, event) updatePreview(); - % changeLineStyleBox.ValueChangedFcn = @(src, event) updatePreview(); - % satelliteMapColorsBox.ValueChangedFcn = @(src, event) updatePreview(); - % legendLocationDropDown.ValueChangedFcn = @(src, event) updatePreview(); - % legendOrientationDropDown.ValueChangedFcn = @(src, event) updatePreview(); - - hold(newAx, 'off'); - end - -% Callback Function for Button - function exportFigureCallback() - % Get values from the GUI components - figName = [prefixIPTNameDropDown.Value figNameField.Value]; - percTextwidth = percTextField.Value; - addMarkers = addMarkersBox.Value; - forcedMarkers = forcedMarkersField.Value; - changeColors = changeColorsBox.Value; - changeLineStyle = changeLineStyleBox.Value; - gridOption = gridBox.Value; - legendLocation = legendLocationDropDown.Value; - legendOrientation = legendOrientationDropDown.Value; - exportPDF = exportPDFBox.Value; - exportFIG = exportFIGBox.Value; - satelliteMapColors = satelliteMapColorsBox.Value; - % figurePath = pathField.Value; - WHratio = WHratioField.Value; - overwriteFigure = overwriteFigureBox.Value; - - % Check for required inputs - if isempty(figName) || isempty(percTextwidth) - uialert(fig, 'Please enter both Figure Name and Percentage Text Width.', 'Input Error'); - return; - end - - % Figure path - figurePath = uigetdir; - - % Cache figure to reset easily - selectedIdx = figSelectionDropDown.Value; - figToExp = figHandles(strcmp(figSelectionDropDown.Items, selectedIdx)); - % - % currentPath = mfilename('fullpath'); - % cachePath = fullfile(currentPath, '..','cachedFigs', selectedIdx); - % savefig(figToExp, cachePath); - - % Try to export figure - try - % Call the exportStandardizedFigure function - exportStandardizedFigure(figToExp, figName, percTextwidth, ... - 'addMarkers', addMarkers, ... - 'forcedMarkers', forcedMarkers, ... - 'changeColors', changeColors, ... - 'changeLineStyle', changeLineStyle, ... - 'grid', gridOption, ... - 'legendLocation', legendLocation, ... - 'legendOrientation', legendOrientation, ... - 'exportPDF', exportPDF, ... - 'exportFIG', exportFIG, ... - 'satelliteMapColors', satelliteMapColors, ... - 'figurePath', figurePath, ... - 'WHratio', WHratio, ... - 'overwriteFigure', overwriteFigure); - uialert(fig, 'Figure exported successfully!', 'Success', 'Icon', 'success'); - catch ME - % Display error if the function fails - uialert(fig, ['Error exporting figure: ' ME.message], 'Error'); - end - end - - function previewFigureCallback() - % Get values from the GUI components - percTextwidth = percTextField.Value; - addMarkers = addMarkersBox.Value; - forcedMarkers = forcedMarkersField.Value; - changeColors = changeColorsBox.Value; - changeLineStyle = changeLineStyleBox.Value; - gridOption = gridBox.Value; - legendLocation = legendLocationDropDown.Value; - legendOrientation = legendOrientationDropDown.Value; - exportPDF = exportPDFBox.Value; - exportFIG = exportFIGBox.Value; - satelliteMapColors = satelliteMapColorsBox.Value; - % figurePath = pathField.Value; - WHratio = WHratioField.Value; - overwriteFigure = overwriteFigureBox.Value; - - selectedIdx = figSelectionDropDown.Value; - figToExp = figHandles(strcmp(figSelectionDropDown.Items, selectedIdx)); - previewStandardizedFigure(figToExp, percTextwidth, ... - 'addMarkers', addMarkers, ... - 'forcedMarkers', forcedMarkers, ... - 'changeColors', changeColors, ... - 'changeLineStyle', changeLineStyle, ... - 'grid', gridOption, ... - 'legendLocation', legendLocation, ... - 'legendOrientation', legendOrientation, ... - 'exportPDF', exportPDF, ... - 'exportFIG', exportFIG, ... - 'satelliteMapColors', satelliteMapColors, ... - 'WHratio', WHratio, ... - 'overwriteFigure', overwriteFigure); - - % checkboxes = findall(flagsPanel.Children, 'Type', 'uicheckbox'); - % set(checkboxes, 'Enable', 'off'); - end - - function saveTemporaryFigure() - % retrieve local path to save temp fig in temp folder - filepath = fileparts(mfilename('fullpath')); - tempPath = [filepath '\temp']; - if ~isfolder(tempPath) - mkdir(tempPath); - end - - figToExp = findFigures(fig); - names = getFigureNames(figToExp); - - for i=1:length(names) - filename = [tempPath '\' char(names(i))]; - savefig(figToExp(i), filename) - end - disp('Temporary .fig available in temp folder'); - - % checkboxes = findall(flagsPanel.Children, 'Type', 'uicheckbox'); - % set(checkboxes, 'Enable', 'off'); - end -end - -function figHandles = findFigures(fig) -figHandles = findall(0, 'Type', 'figure'); -figHandles(figHandles == fig) = []; % Exclude GUI figure -end - -function figNames = getFigureNames(figHandles) -if isempty(figHandles) - figNames = {'No figures open'}; -else - figNames = arrayfun(@(f) sprintf('%s', f.Name), figHandles, 'UniformOutput', false); - % Replace empty names with "Figure #" - emptyNames = cellfun(@isempty, figNames); - figNames(emptyNames) = arrayfun(@(f) ... - sprintf('Figure %d', f.Number), figHandles(emptyNames), 'UniformOutput', false); -end -end + 'ButtonPushedFcn', @(src, event) resetToDefaultAll(box, dropDown, inputFields)); -%% Further developments -% uilabel(fig, 'Text', 'Figure Save Path:', 'Position', [50 70 150 30]); -% pathField = uitextarea(fig, 'Position', [200 70 300 30]); \ No newline at end of file +end \ No newline at end of file -- GitLab