Hello Pi_etudiant,
It seems you're attempting to refresh the mean plot line in your chart whenever you change the visibility of specific lines (which symbolize contractions) by interacting with the legend entries. I've identified a couple of potential concerns within your hitcallback_ex1 function that might be contributing to the difficulties you're facing.
- Utilization of Global Variables: The practice of employing global variables, including mean_plot and ContractionsExt, might yield unpredictable results, particularly when they are altered across various sections of your code. A more reliable approach would involve passing these variables as parameters to your functions or incorporating them into UserData or appdata.
- Construction of the Visibility Array: You're trying to generate a logical array to represent the visible contractions with the expression visibleContractions = [contractionVisibility.values{:}];. However, it's possible that the array isn't being formed as you expect. Verify that the map's keys in contractionVisibility correspond precisely with the DisplayName of each line.
- Refreshing the Mean Line: Be cautious to select the appropriate plot handle when you're refreshing the mean line, and confirm that you're modifying the intended line.
Here is the revised version of the code , I belive it should resolve your issue.
function hitcallback_ex1(src, evnt, fig)
figUserData = get(fig, 'UserData');
ContractionsExt = figUserData.PlotData.ContractionsExt; % Get ContractionsExt from UserData
Angles = figUserData.PlotData.Angles; % Get Angles from UserData
% Toggle the visibility of the selected line
if strcmp(evnt.Peer.Visible, 'on')
evnt.Peer.Visible = 'off';
else
evnt.Peer.Visible = 'on';
end
% Get all line handles and their corresponding display names
hLines = findobj(gca, 'Type', 'line');
lineDisplayNames = {hLines.DisplayName};
% Determine which lines are visible
visibleLines = strcmp({hLines.Visible}, 'on');
% Calculate the mean for visible contractions
visibleContractions = ContractionsExt(:, visibleLines);
mean_plot = mean(visibleContractions, 2, 'omitnan');
% Find the handle of the mean plot line
hMeanLine = findobj(gca, 'Type', 'line', 'DisplayName', 'Moyenne');
% Update the mean plot line
if ~isempty(hMeanLine)
set(hMeanLine, 'YData', mean_plot);
else
% If the mean line does not exist, create it
hold on;
plot(Angles, mean_plot, 'k', 'LineStyle', '--', 'LineWidth', 1.8, 'DisplayName', 'Moyenne');
hold off;
end
% Update the UserData with the new mean plot
figUserData.mean_plot = mean_plot;
set(fig, 'UserData', figUserData);
end
I hope it helps!