Refresh data in plot

3 次查看(过去 30 天)
Pi_etudiant
Pi_etudiant 2024-1-29
I ty to refresh my mean in my plot, depending on my legends visibility but it doesn't works.
My ligne of code :
Data_Traitee = Choix_ContractionsEXCCON(Data_Traitee);
My functions :
function Data_Traitee = Choix_ContractionsEXCCON(Data_Traitee)
global visibilite mean_plot ContractionsExt
for j = 1: length(Data_Traitee)
fig = figure('units','normalized','outerposition',[0 0 1 1 ]);
%..............a part of my code
plot(Angles(:,i), ContractionsExt(:,i),'Color', color(i),'DisplayName', LegendStrings(i),'LineWidth',2);
userData.PlotData = struct('Angles', Angles, 'ContractionsExt', ContractionsExt, 'LegendStrings', LegendStrings);
userData.SelectionFinie = false; % definie le bouton selection fini faux pour que quand on clique dessus il soit vrai et que ça passe à l'action suivante uniquement à ce moment là
set(fig, 'UserData', userData);
mean_plot = mean(ContractionsExt,2);
plot(Angles,mean_plot,'k','LineStyle','--','LineWidth',1.8); % ajout de la moyenne
%........................a part of my code
l.ItemHitFcn = @(src, event) hitcallback_ex1(src, event, fig); % callback de la legende si on clique dessus la contraction n'est plus ploté
%.......................a part of my code
Data_Traitee(j).ContractionsExt_Interp_Select(sum((Data_Traitee(j).ContractionsExt_Interp_Select==0),2)>0,:) = [];
clearvars l LegendStrings ContractionsExt Angles
% Vérifier si le bouton "Sélection finie" a été sélectionné :
if userData.SelectionFinie
% Le bouton a été sélectionné, passez à l'itération suivante
else
% Le bouton n'a pas été sélectionné, ignorez cette itération
continue;
end
% Réinitialiser la propriété "SelectionFinie" pour la prochaine itération
setappdata(fig, 'SelectionFinie', false);
clearvars userData
end
end
function hitcallback_ex1(src, evnt, fig)
global mean_plot ContractionsExt
figUserData = get(fig, 'UserData');
% Récupérer les poignées des lignes tracées
hLines = findobj(gca, 'Type', 'line');
if strcmp(evnt.Peer.Visible, 'on')
evnt.Peer.Visible = 'off';
else
evnt.Peer.Visible = 'on';
end
% Enregistrer l'état de visibilité des contractions dans UserData
contractionVisibility = containers.Map('KeyType', 'char', 'ValueType', 'logical');
for i = 1:numel(hLines)
lineDisplayName = hLines(i).DisplayName;
contractionVisibility(lineDisplayName) = strcmp(hLines(i).Visible, 'on');
end
% Mettre à jour la moyenne en fonction de la visibilité
visibleContractions = [contractionVisibility.values{:}];
mean_plot = mean(ContractionsExt(:, visibleContractions), 2);
% Trouver le handle de la ligne moyenne
hMeanLine = findobj(gca, 'DisplayName', 'Moyenne');
% Mettre à jour la ligne moyenne
set(hMeanLine, 'YData', mean_plot);
figUserData.ContractionVisibility = contractionVisibility;
set(fig, 'UserData', figUserData)
end
My code is with many funtions maybe it's the problem ? I think my problem is in the end of my function hitcallback_ex1. Can somebody help ?

回答(1 个)

prabhat kumar sharma
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.
  1. 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.
  2. 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.
  3. 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!

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by