From 9ec9076df0f5859b81d75f832e68571443365617 Mon Sep 17 00:00:00 2001
From: giuliaghirardini <giulia.ghirardini@skywarder.eu>
Date: Wed, 9 Apr 2025 17:47:55 +0200
Subject: [PATCH] [esf] Solved an ugly bug :), figures handles were messed up
 because the drop down menu automatically changes the order when selecting
 figures

---
 .../fct/exportFigureCallback.m                | 117 ++++++++++--------
 .../fct/previewFigureCallback.m               |  72 ++++++-----
 .../fct/updatePreview.m                       |  50 +++++---
 .../fct/updatePreviewCallback.m               |  38 ++++++
 .../mainExportStandardizedFigure.m            |   6 +-
 5 files changed, 178 insertions(+), 105 deletions(-)
 create mode 100644 functions/utilities/exportStandardizedFigure/fct/updatePreviewCallback.m

diff --git a/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m b/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m
index 2fd7904..56a22f3 100644
--- a/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m
+++ b/functions/utilities/exportStandardizedFigure/fct/exportFigureCallback.m
@@ -1,60 +1,67 @@
+function exportFigureCallback(fig, box, dropDown, inputFields)
 % 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;
+handle_getters = getappdata(fig, 'handle_getters');
+figHandles = findFigures(fig);
+figNames = getFigureNames(figHandles);
+dropDown.figSelection.Items = figNames;
 
-        % Check for required inputs
-        if isempty(figName) || isempty(percTextwidth)
-            uialert(fig, 'Please enter both Figure Name and Percentage Text Width.', 'Input Error');
-            return;
-        end
+% 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;
 
-        % Figure path
-        figurePath = uigetdir;
+% Check for required inputs
+if isempty(figName) || isempty(percTextwidth)
+    uialert(fig, 'Please enter both Figure Name and Percentage Text Width.', 'Input Error');
+    return;
+end
 
-        % 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);
+% Figure path
+figurePath = uigetdir;
 
-        % 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
+% selectedIdx = dropDown.figSelection.Value;
+selectedIdx = handle_getters.Value;
+figHandles = findFigures(fig);
+%figToExp = figHandles(strcmp(dropDown.figSelection.Items, selectedIdx));
+figToExp = figHandles(strcmp(dropDown.figSelection.Items, selectedIdx));
+disp(['Fig to exp: ', 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/previewFigureCallback.m b/functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m
index 28e3278..abafc4a 100644
--- a/functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m
+++ b/functions/utilities/exportStandardizedFigure/fct/previewFigureCallback.m
@@ -1,35 +1,43 @@
 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;
+% make setter global
+handle_getters = getappdata(fig, 'handle_getters');
+figHandles = findFigures(fig);
+figNames = getFigureNames(figHandles);
+dropDown.figSelection.Items = figNames;
 
-        selectedIdx = dropDown.figSelection.Value;
-        figHandles = findFigures(fig);
-        figToExp = figHandles(strcmp(dropDown.figSelection.Items, selectedIdx));
+% 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;
 
-        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
+%selectedIdx = dropDown.figSelection.Value;
+selectedIdx = handle_getters.Value;
+figHandles = findFigures(fig);
+figToExp = figHandles(strcmp(dropDown.figSelection.Items, selectedIdx));
+disp(['Fig to preview: ', 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/updatePreview.m b/functions/utilities/exportStandardizedFigure/fct/updatePreview.m
index 26a7db5..1b7a09e 100644
--- a/functions/utilities/exportStandardizedFigure/fct/updatePreview.m
+++ b/functions/utilities/exportStandardizedFigure/fct/updatePreview.m
@@ -9,29 +9,49 @@ figHandles = findFigures(fig);
 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.width/5;
-    topOffsetNew = screen.height/3;
-
-    positionCurrentFig = [screen.width-width-leftOffsetNew, ...
-        screen.height-height-topOffsetNew, width, height];
-    set(figSeries, 'Position', positionCurrentFig);
-end
+% 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.width/5;
+%     topOffsetNew = screen.height/3;
+% 
+%     positionCurrentFig = [screen.width-width-leftOffsetNew, ...
+%         screen.height-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;
+        if isempty(selectedFig.Name)
+            selectedFig.Name = selectedIdx;
+            %selectedFig = figHandles(i);
+            % update the dropdown names
+            % figNames = getFigureNames(figHandles);
+            % dropDown.figSelection.Items = figNames;
+            break;
+        else
+            break;
+        end
     end
 end
 % Move from background to foreground current figure
+disp(['Selected figure: ',selectedFig.Name])
+disp(' ')
+
+pos = get(selectedFig, 'Position');
+width = pos(3);
+height = pos(4);
+leftOffsetNew = screen.width/5;
+topOffsetNew = screen.height/3;
+positionCurrentFig = [screen.width - width - leftOffsetNew, ...
+    screen.height - height - topOffsetNew, width, height];
+set(selectedFig, 'Position', positionCurrentFig);
 uistack(selectedFig,'top')
 
 if isempty(selectedFig) && isscalar(figHandles), selectedFig = figHandles(1); end
diff --git a/functions/utilities/exportStandardizedFigure/fct/updatePreviewCallback.m b/functions/utilities/exportStandardizedFigure/fct/updatePreviewCallback.m
new file mode 100644
index 0000000..d7efff9
--- /dev/null
+++ b/functions/utilities/exportStandardizedFigure/fct/updatePreviewCallback.m
@@ -0,0 +1,38 @@
+function updatePreviewCallback(src, fig, figSelectionDropDown, flagsPanel, screen, dropDown, box, inputFields)
+    % Trova l'handle della figura selezionata dal nome
+    setappdata(fig, 'handle_getters', src);
+    
+    selectedLabel = src.Value;
+    figHandles = findFigures(fig);
+    selectedFig = [];
+
+    for i = 1:length(figHandles)
+        figName = get(figHandles(i), 'Name');
+        if isempty(figName)
+            figLabel = sprintf('Figure %d', figHandles(i).Number);
+        else
+            figLabel = figName;
+        end
+
+        if strcmp(figLabel, selectedLabel)
+            selectedFig = figHandles(i);
+            break;
+        end
+    end
+
+    if isempty(selectedFig), return; end
+    if isempty(selectedFig.Name)
+        selectedFig.Name = ['Figure ',num2str(selectedFig.Number)];
+    end
+    disp(['>>>>>>> Call back report: ',selectedFig.Name])
+
+    dropDown.figSelection = figSelectionDropDown;
+    disp(['>>>>>>> Dropdown report: ',dropDown.figSelection.Value])
+
+    dropDown.Selection_ = selectedFig.Name;
+
+    figNames = getFigureNames(figHandles);
+    dropDown.figSelection.Items = figNames;
+
+    updatePreview(fig, flagsPanel, screen, dropDown, box, inputFields);
+end
diff --git a/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m b/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m
index c04e881..569c4a2 100644
--- a/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m
+++ b/functions/utilities/exportStandardizedFigure/mainExportStandardizedFigure.m
@@ -168,9 +168,9 @@ figSelectionDropDown = uidropdown(fig, ...
 dropDown.prefixIPT = prefixIPTNameDropDown;
 dropDown.figSelection = figSelectionDropDown;
 
-% Set the callback *after* dropDown is complete
-figSelectionDropDown.ValueChangedFcn = @(src, event) updatePreview(fig, ...
-    flagsPanel, screen, dropDown, box, inputFields);
+% Callback with wrapper to handle the figure
+figSelectionDropDown.ValueChangedFcn = @(src, event) updatePreviewCallback(src, fig, ...
+    figSelectionDropDown, flagsPanel, screen, dropDown, box, inputFields);
 
 % Automatically update preview if only one figure exists
 if isscalar(figHandles)
-- 
GitLab